简体   繁体   中英

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

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 bit-wise OR operation on negative decimals?

It's just doing the operation on the binary representations of your numbers. In your case, that appears to be two's complement .

 17 -> 00010001
-15 -> 11110001

As you can see, the bitwise OR of those two numbers is still -15 .

In your comments above, you indicated that you tried this with the two's complement representations, but you must have done something wrong. Here's the step by step:

 15 ->  00001111      // 15 decimal is 00001111 binary
-15 -> ~00001111 + 1  // negation in two's complement is equvalent to ~x + 1
-15 ->  11110000 + 1  // do the complement
-15 ->  11110001      // add the 1

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.

   17 = b00010001
  -15 = b11110001 <--- 2s complement
| -15 = b11110001

A bitwise or with a negative number works JUST like a bitwise or with a positive number. The bits in one number are ored with the bits in the other number. How your processor represents negative numbers is a different matter. Most use something called "two's complement", which is essentially "invert the number and add 1".

So, if we have, for simplicity, 8 bit numbers:

15 is            00001111
Inverted we get  11110000
Add one          11110001

17 is            00010001

Ored together    11110001

The operator | is a "bitwise OR " operator, meaning that every bit in the target is computed as the OR -combination of the corresponding bits in the two operands. This means, that a bit in the result is 1 if any of the two bits in the numbers at the same positions are 1 , otherwise 0 .

Clearly, the result depends on the binary representation of the numbers which again depends on the platform.

Almost all platforms use the Two's complement , which can be thought as a circle of unsigned numbers, in which negative numbers are just in the opposite direction than positive numbers and "wrap around" the circle.

Unsigned integers:

在此输入图像描述

Signed integers:

在此输入图像描述

The calculation of your example is as follows.

 17:   00000000 00000000 00000000 00010001
-15:   11111111 11111111 11111111 11110001
------------------------------------------
-15:   11111111 11111111 11111111 11110001

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM