Bitwise/Binary operations in Python < 2.1
Alex Martelli
aleax at aleax.it
Tue Apr 9 12:43:01 EDT 2002
petro wrote:
...
>>>def extractPriorityAndFacility(single32bitQuantity):
>>> priority = single32bitQuantity & 7
>>> facility = (single32bitQuantity >> 3) & 0xFFFFFFF
>>Depending on the meaning of "top" ... ^-- might need 4 here??
>>> return priority, facility
>>Just a nit, but I suppose it could make a difference.
>
> 3 gives the right answers:
>
> <22> is generated by postfix, and gives 6 (info) for priority, and 2
> (SMTP) for facility. <62> is generated by Leafnode, and gives 6 for
> priority, 7 (news) for facility.
>
> Thanks, that did the trick.
>
> Could one of you that to me? I don't really understand how that
> worked.
Operator >> shifts a number rightwards (looking at the number as
a string of bit), and operator & applies a "mask" (a bit-by-bit
AND). Are you familiar with bit manipulations at all?
Alex
More information about the Python-list
mailing list