I’ve been trying to emulate in C# a behavior which I was implementing without any problems in C++. I have a flag collection, which based on powers of 2, and a flag which I need to check if it exists in the collection.
byte flagCollection = 1 | 2 | 4 | 8;
byte flag = 2;
if ((flagCollection & flag) != 0) MessageBox.Show("y"); else MessageBox.Show("n");
Now, the problem is that whatever I set Flag to (2, 3, 100, 211), I always get “y”, except for 0. Of course, 1, 2, 4, 8 would be some constants which have various meanings in my application.
In C++:
if (flagCollection & flag)
Would result in TRUE for the variables above.
The way you are doing it seems correct. I’m not really sure what behavior you are expecting. Any of the flags you listed (2, 3, 100, 211) do contain an element of 1 | 2 | 4 | 8. If you want to check whether all of them are present you should be doing