Bitwise operations equivalent of greater than operator

后端 未结 8 1604
花落未央
花落未央 2020-12-05 10:30

I am working on a function that will essentially see which of two ints is larger. The parameters that are passed are 2 32-bit ints. The trick is the only operat

8条回答
  •  自闭症患者
    2020-12-05 11:15

    I think I have a solution with 3 operations:

    Add one to the first number, the subtract it from the largest possible number you can represent (all 1's). Add that number to the second number. If it it overflows, then the first number is less than the second.

    I'm not 100% sure if this is correct. That is you might not need to add 1, and I don't know if it's possible to check for overflow (if not then just reserve the last bit and test if it's 1 at the end.)

提交回复
热议问题