Golang Concise with comments


  • 0
    A
    func getSum(a int, b int) int {
    	for b != 0 { // b is just b the first round, otherwise it's the carry from the previous round
    		c := a & b // This is whatever needs to be carried (i.e. 1b+1b=10b)
    		a ^= b     // This is the addition, without carrying
    		b = c << 1 // Carrying needs to be shifted to the left by one (i.e. 1b+1b=10b (+), not 1b&1b=1b (&)); we replace b
    	}
    	return a
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.