bitwise not - not what I expected

Michael Peuser mpeuser at web.de
Sun Aug 17 23:41:51 CEST 2003


"Dennis Lee Bieber" <wlfraed at ix.netcom.com> schrieb im Newsbeitrag
news:c84511-ol3.ln1 at beastie.ix.netcom.com...
> Elaine Jackson fed this fish to the penguins on Saturday 16 August 2003
> 09:58 pm:
>
> >
> >
> > Is there a function that takes a number with binary numeral a1...an to
> > the number with binary numeral b1...bn, where each bi is 1 if ai is 0,
> > and vice versa? (For example, the function's value at 18 [binary
> > 10010] would be 13
> > [binary 01101].) I thought this was what the tilde operator (~) did,
> > [but when I
> > went to try it I found out that wasn't the case. I discovered by
> > experiment (and verified by looking at the documentation) that the
> > tilde operator takes n to -(n+1). I can't imagine what that has to do
> > with binary numerals.

[..]

>         You've had lots of answers at the moment though I haven't seen
anyone
> explain away the "+1" part...
>
>         Most computers use twos-complement arithmetic to avoid the problem
of
> having two valid values for integer 0, which is what appears in ones
> complement arithmetic.
>
>         For argument, assume an 8-bit integer. The value of "5" would be
> represented as 00000101. The one's complement negative would be
> 11111010. So far there isn't any problem... But consider the value of
> 0, represented as 00000000. A one's complement negative would become
> 11111111 -- But mathematically, +0 = -0; in one's complement math, this
> does not hold true.
>
>         So a little trick is played, to create twos complement... To
negate a
> number, we take the ones complement, and then add 1 to the result. The
> "5" then goes through: 00000101 -> 11111010 + 1 -> 11111011... Looks
> strange, doesn't it... But watch what happens to that 8-bit 0: 00000000
> -> 11111111 + 1 -> (overflows) 00000000.... Negative 0 is the same as
> positive 0.

[..]

I have the impression (may be wrong) that you are working under the
misconception that there can be a "natural" binary represensation of
negative numbers!?
Three conventions have commonly been used so far:
1- Complement
2-Complement
Sign tag plus absolut binary values

All of them have their pros and cons. For a mixture of very technical
reasons (you mentioned the +0/-0 conflict, I might add the use of binary
adders for subtraction) most modern computers use 2-complement, and this now
leads to those funny speculations in this thread. ;-)

Kindly
Michael P






More information about the Python-list mailing list