Add two integers using only bitwise operators?

后端 未结 7 1265
遇见更好的自我
遇见更好的自我 2020-11-27 12:41

In C#, is it possible to perform a sum of two 32-bit integers without using things like if..else, loops etc?

That is, can it be done using only the bitwise operation

7条回答
  •  孤独总比滥情好
    2020-11-27 13:11

    Think about how addition happens bit by bit. Shift the values to get each bit of each operand in turn, then look at the four possible values for the two bits and work out what the result bit should be and whether there's a carry bit to worry about. Then see how the result and carry can be caculated using the bitwise ops.

提交回复
热议问题