Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int sum(int a, int b) {
- int a_bit, b_bit, carry = 0, result=0;
- for(int i=0; i<sizeof(int) * 8; i++) {
- a_bit = (a >> i) & 1;
- b_bit = (b >> i) & 1;
- switch(a_bit + b_bit + carry) {
- case 0:
- result &= ~(1 << i); // clear
- carry = 0;
- break;
- case 1:
- result |= 1 << i; // set
- carry = 0;
- break;
- case 2:
- result &= ~(1 << i); // clear
- carry = 1;
- break;
- case 3:
- result |= 1 << i; // set
- carry = 1;
- }
- }
- return result;
- }
- int main() {
- std::cout << sum(255, 3) << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement