-3

Take a look at this compiler:

https://ideone.com/Y09Z0N

The code is very simple:

cout << ~5; 

And this outputs -6

Now I'm no C++ guru, but somehow I remember that the ~ operator should flip a numbers bits, and since 5 is 101, I would expect to get 010, which is 2, or more precisely 5 is 0000......101 and I should get 1111...010 which should be a really big negative number and not 6 (110). The question is: am I wrong about the operator or am I missing something?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Mario Stoilov
  • 3,411
  • 5
  • 31
  • 51
  • 2
    Why do you expect "a really big negative number"? The binary representation of `-1` is `11...1` (all one bits). See also: https://en.wikipedia.org/wiki/Two%27s_complement – Igor Tandetnik May 28 '17 at 14:21
  • Does some other compiler you tried give results different to the "web compiler"? – juanchopanza May 28 '17 at 14:23
  • The Windows, macOS, or Linux Calculator apps would also give you the same result. And they would show you the binary bit patterns that produced those results, which should help you to visualize what's happening. – Cody Gray - on strike May 28 '17 at 14:26

2 Answers2

4

Negative integers are typically represented in two's complement. This allows basic operations such as addition and subtraction to work with negative numbers in the exactly the same way as positive ones. In that form, negatives are represented as:

00000000 = 0
11111111 = -1
11111110 = -2
11111101 = -3
11111100 = -4
11111011 = -5
11111010 = -6

So indeed -6 = ~5

CygnusX1
  • 20,968
  • 5
  • 65
  • 109
0

This is actually a subtle thing about two's complement.

I will write your numbers in base 16, at the size of the variable (I assume 32 bits, but you can alter the answer for other sizes).

5 is 0x00000005. ~5 is its negation, 0xFFFFFFFA. Add 5 to that, and you see ~5 + 5 is 0xFFFFFFFF. Add one more and you get 0x00000000, or 0, due to overflow.

Two's complement representation has made it so that incrementing past 0x7FFFFFFF (or 2147483647) is the actual overflow (and 0x80000000 is -2147483648), and simply the increment from -1 to 0 has been made not to cause an overflow.

You may read more about two's complement representation and also ask for more info from me if you wish.

Paul Stelian
  • 1,381
  • 9
  • 27
  • I don't see what `~` has to do with overflow. This answer is... confusing... – bolov May 28 '17 at 14:34
  • @bolov That was an additional part of two's complement. About why it is used in hardware? Well the processor doesn't really care if it's signed or not for addition, subtraction and multiplication (except for setting flags, perhaps), since it works identically *only* with this representation. – Paul Stelian May 28 '17 at 16:12
  • I am sorry, your point doesn't get across, at least not for me. Maybe if you reworded the answer. – bolov May 28 '17 at 17:36
  • @bolov No need for that, the question this one got merged into has **much** higher quality answers. – Paul Stelian May 30 '17 at 03:13