bitwise not - not what I expected

Michael Peuser mpeuser at web.de
Sun Aug 17 09:01:43 CEST 2003


"Elaine Jackson" <elainejackson7355 at home.com> schrieb im Newsbeitrag
news:1YD%a.747879$3C2.17342633 at news3.calgary.shaw.ca...


...

> I'm writing a script that will
> play Nim, just as a way of familiarizing myself with bitwise operators.
Good
> thing, too: I thought I understood them, but apparently I don't.)
>
Hi Elaine,
You have described a general misconception you seem to be not the only one
to live with.
The enlightening answers having been posted might sufficwe, but a schuld
liek to add some more "enlightenment":
Bit complements have a lot to do with set complents and the aritmetic
negation (sometimes called two's complement for obvious reasons). Consider
the  set of of "red" of "blue". Now whats the complement? "green" and
"yellow" is obviously the wrong answer. You in fact cannot give any answer
befor you define the total set you are dealing with. The same applies to
logical bit operations. Generally you take a "processor" word or a part of
it to be defined. Some high level languages are more flexible; and even some
computers ("vector processors") are.

The only rule is, that ~(~x) == x

The same situation with numbers: What is the negation of +5. You have to
think very hard! This is a trick question and you probably will give a
"trick answer":  -5. You should be aware that this is just a trick. "-5"
contains no other information as that it is some "complement" of 5. (same
with complex "imaginary" numbers: 5j (in Python) just says it is some fancy
5.)

Now we define a transformation between positive numbers and bit patterns 5 =
LoL. Note that 5 == ...000005  or LoL == ....ooooLoL does not help any
understanding so you generally skip this part.

Now you do some arithmetic "inversion":  5  ->   -5 This however can (and
should) stay a secret of the processor! By no means should you be interested
in how the machine represents "-5". If  you are courious then know that
there had been times when computers represente -5 as ...LLLoLo. Yes it
worked! And you had two diffrent "zeros" then: +0 and -0 !!!!

Most computers do not distinguish between the representation of negativ
numbers and complemented sets (let alone note a special "total set" the
complemt was referring to). Thus the "secret" of modern two's-complement
computern arithmetic is always disclosed to you.

Note that there is no use in something like "masking" the MSB, i.e. that
bits-complements only work on 31 bits. This will lead to ~5 == ~LoL == ~
LL..LLLLoL ==  2,147,483,643 Not much improvement, eh!?

Kindly
Michael P






More information about the Python-list mailing list