Tim Peters <tim.pet...@gmail.com> added the comment:

Terry, the language reference also says:

"""
For the purpose of shift and mask operations, a binary representation is
assumed, and negative numbers are represented in a variant of 2's
complement which gives the illusion of an infinite string of sign bits
extending to the left.
"""

That explains every result you saw:

 3 = ...000011
 2 = ...000010
 1 = ...000001

-3 = ...111101
 2 = ...000010
-1 = ...111111

 3 = ...000011
-2 = ...111110
-3 = ...111101

-3 = ...111101
-2 = ...111110
 3 = ...000011

 2 = ...000010
 3 = ...000011
 1 = ...000001

-2 = ...111110
 3 = ...000011
-3 = ...111101

In every case, the result is simply the xor of the inputs viewed as
infinite bitstrings.  And it works exactly the same way if you use |, &,
<<, >>, or unary ~.

It's true that CPython's longs are /implemented/ via sign-magnitude, but
that should never be visible in the result of any operation.

----------
nosy: +tim_one

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue7406>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to