Bitwise 'AND' error?
tim at digicool.com
Thu Jun 21 23:54:31 CEST 2001
> 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
> >>> long
> 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
More information about the Python-list