Beetwise

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 it

The result of 12 AND 10. Select a bit to change it. Change the bit width or the signed mode to see the effect.

Name or expression
ID0
Binary
0b
Hex
0x
Decimal
ASCII
·
Shift
Sign
Bits
Endian
7
6
5
4
3
2
1
0

Worked example

Calculate 12 AND 10:

  1. Write the first value: 1100.
  2. Write the second value: 1010.
  3. Compare each position. Only bit 3 has the value 1 in both values.
  4. The result is 1000, which is 8.

Where this 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.

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 tool

The full editor holds many values at the same time. It also evaluates expressions across them, and it reads live values from a serial port.

Open the full editor

Questions

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.