-1

I am trying the following code and here are the outputs, anyone have ideas why -10 and -11 are returned?

print ~9
print ~10

-10
-11

BTW, I am using Python 2.7.8.

S.B
  • 13,077
  • 10
  • 22
  • 49
Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • 1
    @ajcr: well, ones complement. – Jeff Mercado Oct 02 '15 at 21:23
  • Thanks @ajcr, Why 9 complement with -10, other than -9? :) – Lin Ma Oct 02 '15 at 21:23
  • @JeffMercado, thanks, and why 10 complement with -11, other than -10? :) – Lin Ma Oct 02 '15 at 21:24
  • 2
    `~n` is two's complement, returning `-(n + 1)`. So ~9 is -10, etc. – Martijn Pieters Oct 02 '15 at 21:24
  • @MartijnPieters, what means two's complement? For example? – Lin Ma Oct 02 '15 at 21:25
  • 1
    @LinMa: see [Wikipedia](https://en.wikipedia.org/wiki/Two%27s_complement) – Martijn Pieters Oct 02 '15 at 21:25
  • 1
    If you test with more numbers like `~1, ~2, ~3, ~4, ~5` you realize the delta between them all is `-1` always. It look like a simetry shifted to `-1` instead of `0`. – Shapi Oct 02 '15 at 21:29
  • @MartijnPieters, thanks for sharing the twiki, and very informative to learned a lot. 2' complement means x + y = 2^N. I do not quite catch why 9 complement -10? Their addition is not 2^N? Please feel free to correct me if I am wrong. – Lin Ma Oct 02 '15 at 21:45
  • @andre.leitao.developer, thanks for sharing the thoughts, and any further thoughts why shift by -1 other than 0 is appreciated. :) – Lin Ma Oct 02 '15 at 21:46
  • 1
    you are wellcome :) @LinMa – Shapi Oct 02 '15 at 21:47
  • 2
    @LinMa: the `~` operator produces a two's complement binary number. `10` decimal is `1010` in binary; add a leading bit for the sign making `01010`. Flip the bits and you get `10101`, which in two's complement is `-11` decimal. It is *that* number that is subtracted from 2^N; there are 5 bits, so 2^5 is 32. `10101` in binary (without two's complement) is 21. 32 - 21 is 11, but negative, so negative eleven. – Martijn Pieters Oct 02 '15 at 21:57
  • Thanks @MartijnPieters, I think maybe why ~9 is -10 could be calculated in this way (https://en.wikipedia.org/wiki/Two%27s_complement#Most_negative_number), see table on the right for invert bits and add one? And lost why "10101, which in two's complement is -11 decimal"? – Lin Ma Oct 02 '15 at 22:17

1 Answers1

2

From: Python Doc

The unary ~ (invert) operator yields the bitwise inversion of its plain or long integer argument. The bitwise inversion of x is defined as -(x+1). It only applies to integral numbers.

From: Python Doc

Two's Complement binary for Negative Integers:

Negative numbers are written with a leading one instead of a leading zero. So if you are using only 8 bits for your twos-complement numbers, then you treat patterns from "00000000" to "01111111" as the whole numbers from 0 to 127, and reserve "1xxxxxxx" for writing negative numbers. A negative number, -x, is written using the bit pattern for (x-1) with all of the bits complemented (switched from 1 to 0 or 0 to 1). So -1 is complement(1 - 1) = complement(0) = "11111111", and -10 is complement(10 - 1) = complement(9) = complement("00001001") = "11110110". This means that negative numbers go all the way down to -128 ("10000000").

~x Returns the complement of x - the number you get by switching each 1 for a 0 and each 0 for a 1. This is the same as -x - 1.

augre
  • 330
  • 3
  • 12