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?

- 4,147
- 2
- 22
- 49

- 334
- 1
- 4
- 11
-
82's complement....? – Eitan T Jan 14 '13 at 21:23
-
3Technically, I think it's implementation-dependent. – Frédéric Hamidi Jan 14 '13 at 21:23
-
It probably does the `OR` on the value's plain two's-complement data.. – lethal-guitar Jan 14 '13 at 21:23
-
@FrédéricHamidi, Technically, though you'd be hard-pressed to find an exception. – chris Jan 14 '13 at 21:24
-
I performed the operation on their 2's complement representations but i don't get -15 anyway – Narek Margaryan Jan 14 '13 at 21:25
-
@chris, practically, yes. But do you mean now or in the future? :) – Frédéric Hamidi Jan 14 '13 at 21:25
-
3@user1978522 Really? Then you did it wrong ;) – Nik Bougalis Jan 14 '13 at 21:26
-
If you didn't get -15 as an answer, then you did something wrong. Maybe you didn't convert to two's-complement form correctly. Or maybe you had some bits misaligned. – Rob Kennedy Jan 14 '13 at 21:27
-
this is such a fastest gun question – Sam I am says Reinstate Monica Jan 14 '13 at 21:29
-
1@FrédéricHamidi Yes, according to the C++ standard (in 3.9.1.7) it is implementation dependent to support architectures that are not necessarily 2's complement. However, every architecture in the past few decades has been 2's complement-based (since it simplifies the hardware) so it is more than a safe bet. – Sean Cline Jan 14 '13 at 21:32
-
@FrédéricHamidi, Now, I guess. What'd coming in the future? O_o – chris Jan 14 '13 at 21:32
-
1@chris, well, nobody knows, and that's why the C++ language leaves the binary representation of signed integers for the implementation to decide. I guess my point was that almost all of them may be using two's complement now, but who knows about later? – Frédéric Hamidi Jan 14 '13 at 21:36
6 Answers
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

- 258,201
- 41
- 486
- 479

- 219,201
- 40
- 422
- 469
-
Can I know any example where we need to use bitwise operations on negative numbers? I thought bitwise is usually on unsigned numbers. – Jon Wheelock Apr 09 '16 at 05:50
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.

- 161,384
- 21
- 275
- 467
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

- 126,704
- 14
- 140
- 227
17 = b00010001
-15 = b11110001 <--- 2s complement
| -15 = b11110001

- 219,201
- 40
- 422
- 469

- 4,147
- 2
- 22
- 49
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

- 219,201
- 40
- 422
- 469

- 30,851
- 12
- 72
- 100
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

- 44,967
- 21
- 135
- 183
-
-
@AlexChamberlain I don't know for sure. If there is a platform which supports multiple different representations, there could exist two different compilers using two different representations. However, I don't think that such a platform exists. – leemes Jan 14 '13 at 21:52
-
I think the C ABI would impose a consistent representation on any one platform tbh. – Alex Chamberlain Jan 14 '13 at 21:59
-