Bitwise 'AND' error?
Rich J
crj003 at yahoo.com
Fri Jun 22 12:35:05 EDT 2001
"Tim Peters" <tim at digicool.com> wrote in message news:<mailman.993160533.19729.python-list at python.org>...
> [Rich J]
> > Could someone please explain to me why this is giving these results.
> > This is taken directly from the python interpreter.
> >
> > >>> long = 0x2964619C7L
> > >>> mask = 0xFFFFFFFF
> > >>> ling = long & mask
> > >>> print 'new number = %#x' % ling
> new number = 0x2964619c7
> > >>> ling
> 11111111111L
> > >>> long
> 11111111111L
> > >>>
> >
> > Why would 'anding' with this mask give back the same number? It should
> > have given back 0x964619C7, right? Or am I losing my mind?
>
> No, you're running on a 32-bit machine and mixing unbounded ints with 32-bit
> ints. Mask is -1, and in "long & mask" mask has to be converted to long
> first; long(-1) == -1L, i.e. mask is coerced to a (conceptually) infinite
> string of 1 bits. Do
>
> mask = 0xFFFFFFFFL
> ^
> instead.
Thanks, alot! I thought I was losing it!
More information about the Python-list
mailing list