Bitwise/Binary operations in Python < 2.1

Alex Martelli aleax at aleax.it
Mon Apr 8 07:36:10 EDT 2002


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
> * (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
    return priority, facility



Alex





More information about the Python-list mailing list