Bitwise AND
The AND operation compares the two values bit by bit.
A bit in the result has the value 1 only if both inputs have the value 1 at that position.
AND is the operation for a test of one or more bits.
Try bitwise AND in the bit editor
The result of 12 AND 10. Edit the expression or select a bit to flip it.
7
6
5
4
3
2
1
0
8
value · 8 bits · unsigned
- hex
- 0x08
Bitwise AND step by step
Calculate 12 AND 10:
- Write the first value: 1100.
- Write the second value: 1010.
- Compare each position. Only bit 3 has the value 1 in both values.
- The result is 1000, which is 8.
The truth table of AND
| A | B | A & B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Where bitwise AND is used
A driver uses AND with a mask to read one field of a register.
A test of a single flag is an AND with a mask that has one bit set.
AND with 0xFF keeps the lowest byte and clears the rest.
Bitwise AND: points to note
- AND never sets a bit that was clear in both inputs. The result is always equal to or smaller than each input.
- In C, & is the bitwise AND and && is the logical AND. The two give different results.
- AND with 0 always gives 0.
The full bit editor
The full editor holds many values, evaluates expressions across them, and reads live values from a serial port.
Open this value in the full editorQuestions about bitwise AND
- What is 12 AND 10?
- The result is 8. The bits are 1100 and 1010. Only bit 3 has the value 1 in both.
- How do I test one bit with AND?
- Apply AND between the value and a mask that has only that bit set. A result of 0 means the bit is clear.
- What is the difference between & and &&?
- The operator & works on every bit. The operator && gives one true or false result.