C++ and_eq Keyword
Example
The and_eq keyword gives the same result as the &= operator:
int x = 6;
int y = 6;
x and_eq 3;
y &= 3;
cout << x << "\n";
cout << y;
Definition and Usage
The and_eq keyword is another way to write the &= assignment operator. x and_eq 3 means the same as x &= 3, which means x = x & 3.
The & operator is the bitwise AND 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, so the result is 010, which is 2.
Related Pages
Read more about assignment operators in our C++ Assignment Operators Tutorial.