Overview#Bitwise AND takes two equal-length binary representations and performs the logical AND operation on each pair of the corresponding bits, which is equivalent to multiplying them.
Thus, if both bits in the compared position are 1, the bit in the resulting binary representation is:
- 1 (1 × 1 = 1)
- 0 (1 × 0 = 0 and 0 × 0 = 0).
Bitwise AND example:
Bitwise AND may be used to determine whether a particular bit is set (1) or clear (0).
For example, given a bit pattern 0011 (decimal 3), to determine whether the second bit is set we use a bitwise AND with a bit pattern containing 1 only in the second bit:
Bitwise AND may be used to clear selected bits (or flags) of a register in which each bit represents an individual Boolean state. This technique is an efficient way to store a number of Boolean values using as little memory as possible.
For example, 0110 (decimal 6) can be considered a set of four flags, where the first and fourth flags are clear (0), and the second and third flags are set (1). The second bit may be cleared by using a bitwise AND with the pattern that has a zero only in the second bit: