Skip to content

Commit

Permalink
371
Browse files Browse the repository at this point in the history
  • Loading branch information
TedTran2019 committed Jul 7, 2022
1 parent 51337c4 commit 16d2846
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions ruby/371-Sum-of-Two-Integers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
def get_sum(a, b)
mask = 0xffffffff
until b.zero? # until no carries left
tmp = (a & b) << 1 # carries
a = (a ^ b) & mask # addition w/o carries
b = tmp & mask
end
a = ~(a ^ mask) if a > (mask >> 1)
a
end

# Ugly Solution
def get_sum(a, b)
mask = 0xffffffff # 32 bit maximum
res = 0
carry = 0
32.times do |n|
bit_a = (a >> n) & 1
bit_b = (b >> n) & 1

if (bit_a | bit_b).zero?
if carry == 1
carry -= 1
res |= (1 << n)
end
elsif (bit_a & bit_b) == 1
if carry == 1
res |= (1 << n)
else
carry += 1
end
elsif carry.zero?
res |= (1 << n)
end
end
res &= mask
if (res >> 31) & 1 == 1
# XOR flips rightmost 32, then NOT flips all bits
res = ~(res ^ mask)
end
res
end

0 comments on commit 16d2846

Please sign in to comment.