# ~ 1 = -2 ???

Sat Nov 6 06:52:45 CET 1999

```Les Schaffer <godzilla at netmeg.net> wrote in message
news:m2vh7hk1ys.fsf at netmeg.net...
> i am doing some bit operations, and dont understand the following:
>
> >>> ~ 1
> -2
>
> huh???? i would have thought that bitwise invert of 1 would be
>
> 0001 --> 1110 = 13 (base 10)
>
> what marble am i missing?
>
> les schaffer

Integers are stored in twos-complement format. (Which means...)
The top bit of the integer is treated as having a negative value.
This means that FFFFFFFF is equal to
1 + 2 + 4 + 8 + ... + 1073741824 - 2147483648 = -1
So, 1 is 00000001, inverted is FFFFFFFE, which is
2 + 4 + 8 + 16 + ... + 1073741824 - 2147483648 = -2

This means that the largest positive integer value is 2147483647, while
the largest negative value is -2147483648. It's better than having the
top bit just be a sign bit 'cause it avoids confusing -0 problems. (And
it gives us one more value. Yay!)

HTH, HAND
Xtian

xtian at regurgitator.zzn.com

"As I bit into the nectarine, it had a crisp juicyness that was very
refreshing, until I realised that it wasn't a nectarine at all, but a