How does C++ do bitwise “or” operations on negative numbers?

前端 未结 6 1813
野的像风
野的像风 2021-01-12 16:18

When I give to a variable such value: e = 17|-15; , I get -15 as an answer after compiling.I can\'t understand what arithmetic c++ uses. How does it perform a

6条回答
  •  醉话见心
    2021-01-12 17:11

    you have to looks at how the bits work

    Basically, if either number has a 1 in a particular spot, than the result will also have a 1

    -15       : 11110001 (two's complement)
    17        : 00010001
    -15 | 17  : 11110001
    

    as you can see, the result is the same as -15

提交回复
热议问题