C++ bitand Keyword
Example
The bitand keyword gives the same result as the & operator:
int x = 6;
int y = 3;
cout << (x bitand y) << "\n";
cout << (x & y);
Definition and Usage
The bitand keyword is another way to write the bitwise & operator. It compares the bits (the 1s and 0s that make up a number) of two numbers. A bit in the result is 1 only if the bit is 1 in both numbers.
In the example, 6 is 110 in binary (base 2) and 3 is 011. Only the middle bit is 1 in both, so the result is 010, which is 2.
Do not mix up bitand (&) with and (&&). and works with true and false values, while bitand works on each bit.
Related Pages
Read more about operators in our C++ Operators Tutorial.