[Tutor] 2s complement binary for negative
Dave Angel
davea at davea.name
Thu May 1 14:27:42 CEST 2014
Ian D <duxbuz at hotmail.com> Wrote in message:
> Can anyone clarify please?
>
>
> Just reading this:
>
> https://wiki.python.org/moin/BitwiseOperators
>
>
> The section on 2's complement binary for negative integers.
>
>
> It states:
>
>
> "Thus the number -5 is treated by bitwise operators as if it were written "...1111111111111111111011". "
>
>
> I am wondering why would this not be -4?
>
>
> I though -5 would be written ..1111111111111010
>
>
> 5 being 101b
>
That would be the case for a machine/language using ones-complement.
http://en.m.wikipedia.org/wiki/Ones'_complement
Twos-complement is usually explained by saying that for negative
integers you take the ones-complement value and add one. But it
might be more correct to say that for ones-complement machines
you take the obvious result when subtracting numbers, and if
it's negative you subtract one from it.
Forty plus years ago I used a ones-complement machine, the CDC
6400, as well as a twos-complement one, the IBM 360. Since then,
every architecture I used, and every one I implemented, were
twos-complement, including all the Intel and AMD and Motorola
ones.
Twos-complement has some advantage in the hardware needed for most
arithmetic, but that doesn't matter to us as users. What does
matter is when we mix arithmetic and logic, on the same value.
And when we support more than one size of integer.
Probably the most visible difference is the fact that there are
two values for zero in ones-complement, all zero bits, and all
one bits. When doing arithmetic, it's normal to consider them
equal, but when doing bitwise operations they're clearly
different. In fact on the CDC, False and True are usually +0 and
-0. So on that architecture python would need two equality
operators. According to the page you referenced, Python chose
to hide the underlying architecture when running on an occasional
ones-complement machine. Good for them.
--
DaveA
More information about the Tutor
mailing list