371. Sum of Two Integers

Source code notebook Author Update time

Calculate the sum of two integers a and b , but you are not allowed to use the operator + and -.

Example 1:

Input: a = 1, b = 2
Output: 3

Example 2:

Input: a = -2, b = 3
Output: 1
# @lc code=start
using LeetCode

function get_sum371(a::Int, b::Int)::Int
    while b != 0
        carry = (a & b) << 1
        a = a ⊻ b
        b = carry
    end
    a
end
# @lc code=end
get_sum371 (generic function with 1 method)

This page was generated using DemoCards.jl and Literate.jl.