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;


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.

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 )

Facebook photo

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

Connecting to %s