C++ solution use bit manipulation


  • 0
    D
    class Solution {
    public:
    int getSum(int a, int b) { 
        long long result = 0;
        int CF = 0;
        int bitmask = 1;
        for( int i = 0; i < 32; i++ ){
             result |= (a&(bitmask<<i))^(b&(bitmask<<i))^(CF<<i);
             CF = (((a&(bitmask<<i))&(b&(bitmask<<i)))|((a&(bitmask<<i))&(CF<<i)) | ((b&(bitmask<<i))&CF<<i)) != 0 ? 1 : 0;  
        }
      return result;  
    }
    };

Log in to reply
 

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