Bitwise 'AND' error?
Tim Peters
tim at digicool.com
Thu Jun 21 17:54:31 EDT 2001
[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.
More information about the Python-list
mailing list