[LeetCode]#371. Sum of Two Integers

Input: a = 1, b = 2
Output: 3
Input: a = -2, b = 3
Output: 1
class Solution:
def getSum(self, a, b):
mask = 0xffffffff
sum = (a ^ b) & mask
carry = a & b
while carry!=0:
a = sum
b = (carry << 1) & mask
sum = (a ^ b) & mask
carry = a & b
if sum & 0x80000000:
sum -= 0x100000000
return sum

--

--

--

Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Nimbella — The next gen ServerLess and Stateful Computing

Learn Solana in Hindi

Review: Black Opal’s “Programming” by Q-CTRL

Algorithm Practice: Find Averages of Subarrays

The Art of Maintaining a Backlog

Why Manage WIP?

Azure Multi-Stage Pipeline for Asp.Net Core and Azure, Deploy to Azure App Service

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.

More from Medium

LeetCode Patterns Adventure 16 — Merge Two Sorted Lists

Sorting Algorithms Explained Using Python: Merge Sort

Glass Cube and Dots

Leetcode Python 刷題記錄 Day 1

Leetcode — Partition to K Equal Sum Subsets