[Python Edinburgh] Pub Meetup Tomorrow: Who's Coming?

Mark Smith mark.smith at practicalpoetry.co.uk
Tue Oct 26 15:19:00 CEST 2010


Sorry, I'm getting a bit carried away now. You can generate the two binary
numbers you need using the bitmask function below. I used to have a load of
these simple bit-twiddling functions when I was at ST. Unfortunately I
stopped using them some time ago and didn't take a copy when I left.

def bitmask(high_bits):
    """Takes a list of bit-indexes you want to be high, and returns an int
       with those bits high and all others low."""
    result = 0
    for i in high_bits:
        result |= (1 << i)
    return result

mask = bitmask([3,0])           # Bits you want to test (we want to ensure 3
is high, 0 is low)
expected_value = bitmask([3])   # Bits you want to be high (we want to
ensure 3 is high, all others are low)
if (y & mask) == expected_value:
    print 'yay!'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edinburgh/attachments/20101026/90552105/attachment.html>


More information about the Edinburgh mailing list