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

John Sutherland john at sneeu.com
Tue Oct 26 14:52:27 CEST 2010


On Tue, Oct 26, 2010 at 1:04 PM, Ed HAWKINS <ed.hawkins at st.com> wrote:
> Simple(?) python question:
>
> Suppose I have a variable, y, which contains a binary number (4 bits,
> (3:0)). I want to compare individual bits within y and return True if some
> of the bits are set/clear.
>
> For example, suppose I want to check bit3 is set and bit0 is clear... At
> present I have:
>
> (y & 0x08) == 8 and (y & 0x01) == 0
>
> which seems a bit clunky (especially since I want to set up a dictionary of
> possible compares). Is there a better way to do it?
>
> I suppose I really want to match the ‘pattern’ 0b1XX0 with y (where X is
> don’t care)...

I’m not a bit twiddler, but I don’t think you need the octal notation,
also 0 is “Falsy”, while all other integers are ”Truthy”, so the
following could be:

    y & 8 and not y & 1

John.


-- 
sneeu.com
"A computer shall not waste your time or require you to do more work than is
strictly necessary" – Jef Raskin.


More information about the Edinburgh mailing list