Weird 'and' result...

Mike C. Fletcher mcfletch at
Sat Jun 21 01:34:41 CEST 2003

Artur M. Piwko wrote:

>pipen at demon:/tmp/ekg-1.1rc1$ python
>Python 2.2.3 (#1, Jun  4 2003, 02:54:59)
>[GCC 3.3 (Debian)] on linux2
>Type "help", "copyright", "credits" or "license" for more information.
>>>>"%08x" % (0xea7ee5ccea7 & 0xffffffff)
>And it should be 'ee5ccea7'. Am I missing something?
Coercian to long to & with the left operand occurs *after* 0xffffffff is 
interpreted as regular integer -1.  So, you're asking for:

0xea7ee5ccea7 & -1L

And the -1 is intepreted as an "infinite stream of 1 bits" (or whatever 
Tim thinks of it as), so you get all the bits in your (long) left 
operand returned.  You probably want this:

 >>> "%08x" % (0xea7ee5ccea7L & 0xffffffffL)

That is, explicitly make both numbers long integers.


  Mike C. Fletcher
  Designer, VR Plumber, Coder

More information about the Python-list mailing list