~ 1 = -2 ???

William Tanksley wtanksle at hawking.armored.net
Thu Nov 4 16:07:33 EST 1999


On Fri, 05 Nov 1999 04:01:22 GMT, Les Schaffer wrote:
>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?

The marble about 32-bit twos' complement signed integers.  They're not
four-bit, BTW.

To find the negative in twos' complement, which Python uses, you NOT the
bits and add one.  So if you only NOT the bits, you'll get one less than
the negative of the number you started with.  Try ~-5 for an example.

The bit pattern is still what you need if you're worried about bit
patterns, though.  Personally, ALL of my coding uses unsigned numbers.

>les schaffer

-- 
-William "Billy" Tanksley




More information about the Python-list mailing list