&
Description
The bitwise AND operator in C++ is a single ampersand &, used between two other integer expressions. Bitwise AND operates on each bit position of the surrounding expressions independently, according to this rule: if both input bits are 1, the resulting output is 1, otherwise the output is 0.
Another way of expressing this is:
In Arduino, the type int is a 16-bit value, so using & between two int expressions causes 16 simultaneous AND operations to occur.
Example Code
In a code fragment like:
Each of the 16 bits in a and b are processed by using the bitwise AND, and all 16 resulting bits are stored in c, resulting in the value 01000100 in binary, which is 68 in decimal.
One of the most common uses of bitwise AND is to select a particular bit (or bits) from an integer value, often called masking. See below for an example (AVR architecture specific).
See Also
- Language : << (bitshift left)
- Language : >> (bitshift right)
- Language : ~ (bitwise not)
- Language : | (bitwise or)
- Language : ^ (bitwise xor)
- Language : && Logical AND
- Example : BitMath Tutorial
※ ARDUINO BUY RECOMMENDATION
Arduino UNO R3 | |
Arduino Starter Kit |
Additionally, some links direct to products from our own brand, DIYables.