java carry lookahead adder


  • 0
    W
    public int getSum(int a, int b) {
    	int c = 0;
    	int s = 0;
    	int result = 0;
    	int a2, b2;
    	for (int i = 0; i < 32; ++i) {
    		a2 = (a >> i) & 0b1;
    		b2 = (b >> i) & 0b1;
    		s = a2 ^ b2 ^ c;
    		result |= s << i;
    		c = a2 & b2 | (a2 ^ b2) & c; 
    	}
    	return result;
    }

Log in to reply
 

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