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

前端 未结 6 1771
野的像风
野的像风 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:08

    It does OR operations on negative numbers the same way it does so on positive numbers. The numbers are almost certainly represented in two's-complement form, which gives you these values:

     17 = 0000000000010001
    -15 = 1111111111110001
    

    As you can see, all the bits of 17 are already set in −15, so the result of combining them is again −15.

提交回复
热议问题