How to Check, If Bitmask Contains Bit

How do I check, if bitmask contains bit?

well

if (8 & bitmask == 8 ) {
}

will check if the bitmask contains 8.

more complex

int mask = 8 | 12345;
if (mask & bitmask == mask) {
//true if, and only if, bitmask contains 8 | 12345
}

if (mask & bitmask != 0) {
//true if bitmask contains 8 or 12345 or (8 | 12345)
}

may be interested by enum and more particularly FlagsAttibute.

How to check, if bitmask contains a bit?

Since DEFAULT_ZERO isn’t really a flag (it is the absence of flags) it should need to be treated differently; your proposed way is perfectly reasonable.

Note that you can’t set the DEFAULT_ZERO flag either, again because it isn’t a flag.

Checking specific bits of a bitmask

Integers are nothing but sequence of bits in the computer.

So, if you get integer, let's say: 333 it is a sequence of bits 101001101 to the computer. It doesn't need any unpacking into bits. It is bits.

Therefore, if the mask is also an integer, you don't need any unpacking, just apply bitwise operations to it. Check wikipedia for details of how these work.

In order to check if ANY of the bits xyz are set in an integer abc, you do:
(abc & xyz) > 0. If you absolutely need checking mask to be a tuple of bit places, you do some packing, like this:

def detect(bitmask,check=(18,22,23,24)):
checkmask=sum(2**x for x in check)
if (bitmask & checkmask) > 0:
print "There are some problems"
else:
print "Everything OK"

Note that bitmasks start with 0 based bit indices. First bit is bit 0.

How to efficiently check against bitmask?

If you want to check against one bitmask, then

if ((value & mask) == mask)

will give you an exact match ("all bits in the mask"), and

if ((value & mask) != 0)

will supply a loose match ("any bit in the mask"). The compiler will further optimize the check against zero.

If you have several bitmasks, you want to extract the maximum information out of each check in the time domain (an extreme case: if all the values you get are certainly odd, you needn't check the 0th bit at all. It will always be 1). Ideally you need to identify a first round of bits that have a 50% probability of being 1.

In both groups you then identify a subgroup (probably not the same in the two cases) with the same chance.

if ((value & SPECIAL_MASK_1) == SPECIAL_MASK_1) {
if ((value & SPECIAL_MASK_2) == SPECIAL_MASK_2) {
...
} else {
...
}
} else {
if ((value & SPECIAL_MASK_3) == SPECIAL_MASK_3) {
...
} else {
...
}
}

If you had, say, 32 states, each mapped to one bit, and only one bit can be set at each call - the easiest case - the "serial" sequence would be one of 32 checks one after the other

if ((mask & 0x00000001) == 0x00000001) {
} else if ((mask & 0x00000002) == 0x00000002) {
}
...

and a first simple optimization would to place the checks for the most frequent occurrences first. Say that one case out of three the seventh bit is set; you put the check for the seventh bit first.

This way you will end up doing only one check 33% of the time; then maybe two checks another 20% of the time, ..., and in the end on average you might run, say, seven checks.

Another possibility is

if (mask & 0x0000FFFF) {
// The bit is in the LSW
if (mask & 0x0000FF00) {
// MSB of LSW
if (mask & 0x0000F000) {
...
} else {
}
}
} else {
}

This will run every time exactly five checks. At that point, however, considerations about the CPU architecture, branch prediction etc. are likely to trump any optimization you might attempt to do.

Unless you have a very complex setup, or some other constraint (e.g. embedded device), I fear that the cost of analyzing, building, debugging and maintaining an "optimized" versus "brute force" check is likely to more than balance any advantage you could squeeze out of the former.

How can I check if there are any common bits between two bitmasks?

Sure, just use the bitwise-AND operator (&):

var bm3 = bm1 & bm2; // TestFlags.d

This will return a new value which has a flag set for every bit that was set in both bm1 and bm2.

And if you want to see if any bits are set in this new value, you can just compare it to 0:

Console.WriteLine(bm3 != 0); // True

Testing if a bitmask has one and only one flag

If it's only a single flag then == operator is sufficient as you know exactly what value you're looking for. So in your case:

currentFlags == state

would do the job.

If you'd like to check if there are multiple flags set (particular combination) you could build a value using |= operator and then compare it using ==.

Bit masking (javascript): how to check ALL flags

Just compare to the mask:

if ((number & mask) === mask) {
// all bits are set!
}

The only way the result of the & operation will be exactly the same as the value of the mask is when the number has all the bits set. (The test number may have more bits set; if you want to check to see if it's got exactly the same bits set and unset, then that's a simple equality test.)

Bitmask: how to determine if only one bit is set

You can count the number of bits that are set in a non-negative integer value with this code (adapted to JavaScript from this answer):

function countSetBits(i)
{
i = i - ((i >> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
}

It should be much more efficient than examining each bit individually. However, it doesn't work if the sign bit is set in i.

EDIT (all credit to Pointy's comment):

function isPowerOfTwo(i) {
return i > 0 && (i & (i-1)) === 0;
}


Related Topics



Leave a reply



Submit