Created
November 10, 2011 00:30
-
-
Save pioz/1353683 to your computer and use it in GitHub Desktop.
Sum with bitwise boolean and left shift instructions only
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int | |
add (int a, int b) | |
{ | |
int gen = a & b; // carries | |
int pro = a ^ b; // sum | |
gen |= pro & (gen << 1); | |
pro = pro & (pro << 1); | |
gen |= pro & (gen << 2); | |
pro = pro & (pro << 2); | |
gen |= pro & (gen << 4); | |
pro = pro & (pro << 4); | |
gen |= pro & (gen << 8); | |
pro = pro & (pro << 8); | |
gen |= pro & (gen <<16); | |
return a ^ b ^ (gen << 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment