A question of masks

I am quite comfortable with the idea of using bit masks to test variables for specific values. What came up the other day is the need to test a byte for the presence of only a single set bit, where the specific bit is not known.

I wanted to know whether the byte had any of the following values, without caring what the actual value was;

    00000001
    00000010
    00000100
    00001000
    00010000
    00100000
    01000000

The consensus from everyone I’ve discussed this with is that a loop with multiple tests is required, but I can’t help thinking that out there somewhere is someone that has a clever bit mask or mathematical way to test for this with a single expression.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s