Are 'addition' and 'bitwise or' the same in this case?

后端 未结 6 806
醉酒成梦
醉酒成梦 2020-12-16 11:45

Say I have four 32-bit numbers, defined so that their bits don\'t overlap, i.e.

unsigned long int num0 = 0xFF000000;
unsigned long int num1 = 0x00FF0000;
uns         


        
6条回答
  •  Happy的楠姐
    2020-12-16 12:26

    as long as for two numbers num1 and num2 applies num1 & num2 == 0, then follows:

    num1 + num2 == num1 | num2

    the reason for this is, that addition is basically a bitwise XOR, plus carry bit. But as long as there are no carry bits (num1 & num2 == 0) then addition boils down to bitwise XOR, which is (again because of num1 & num2 == 0) in this case logically equivalent to a bitwise OR

提交回复
热议问题