Bitwise/Binary operations in Python < 2.1

Bengt Richter bokr at oz.net
Mon Apr 8 12:55:12 EDT 2002


On Mon, 08 Apr 2002 11:36:10 GMT, Alex Martelli <aleax at aleax.it> wrote:

>petro wrote:
>        ...
>> * priorities/facilities are encoded into a single 32-bit quantity, where
>> the * bottom 3 bits are the priority (0-7) and the top 28 bits are the
>> facility
That leaves one bit out, right? If you really mean _top_ 28 bits starting
with bit 31 (numbering lsb..msb as 0..31), then you will need >> 4 in
place of >> 3 in Alex's code below.

>> * (0-big number).  Both the priorities and the facilities map roughly
>> * one to strings in the syslogd(8) source code.  This mapping is
>> * included in this file.
>> 
>> The problem is, well, basically I'm pretty stupid, and can for the life
>> of me figure out how to do this in such a way that it's portable from
>> Python 1.5.2 to 2.x
>
>what about:
>
>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.

Regards,
Bengt Richter



More information about the Python-list mailing list