I am using bitwise operators to store some boolean values in a variable. I assume i'm storing them appropiately, although here are my assignments:
int bit = 0;
bit |= 1;
bit |= 2;
bit |= 4;
bit |= 8;
What i am unsure of is the checking part. I have a simple knowledge about the difference between logical and bitwise operators. Here's how i check the values:
if ((bit & 1) && (bit & 2) && (bit & 8)) {
std::cout << "a" << std::endl;
}
else {
std::cout << "b" << std::endl;
}
I want to know if that kind of conditional is correct (i've done some tests, but i might be missing something) and also i want to know if i can check multiple bits at the same time, for example:
if (bit && (1 & 2 & 8) {
std::cout << "a" << std::endl;
}
else {
std::cout << "b" << std::endl;
}
I know that the last won't work as desired (at least that's what tests gave me), but i wanted to illustrate my idea.