Take the 2-minute tour ×

I don't quite understand this whole bitmask concept.

Let's say I have a mask:

var bitMask = 8 | 524288;

I undestand that this is how I would combine 8 and 524288, and get 524296.

BUT, how do I go the other way? How do I check my bitmask, to see if it contains 8 and/or 524288?

To make it a bit more complex, let's say the bitmask I have is 18358536 and I need to check if 8 and 524288 are in that bitmask. How on earth would I do that?

share|improve this question
add comment

2 Answers

up vote 10 down vote accepted


if (8 & bitmask == 8 ) {

will check if the bitmask contains 8.

more complex

int mask = 8 | 12345;
if (mask & bitmask == mask) {

may be interested by enum and more particularly FlagsAttibute.

share|improve this answer
add comment

I'm pretty sure (A & B)==B where A is the bitmask and B is whatever you want to check should do.


if((18358536 & 8) == 8) 
    // mask contains 8
share|improve this answer
add comment

Your Answer


