AND Calculator

Calculate the bitwise AND of two numbers and see the result in binary, hex, octal and decimal.

Updated

decimal
Enter to calculate Esc to clear

AND Calculator

Our AND Calculator applies the AND operation to two numbers bit by bit, the same as a & b in code. Type the numbers in decimal, binary, octal or hex, choose a bit width from 1 to 64, and see the result in every format, with A, B and the result lined up so you can see which bits survive.

Quick answer

A bitwise AND keeps a 1 only where both numbers have a 1 in the same position. For 12 (1100) and 10 (1010), only the 8s column has a 1 in both, so 12 AND 10 = 1000 = 8. Enter any two numbers above and the calculator shows the result instantly.

  • Truth table: only 1 AND 1 gives 1; every other pair gives 0
  • In code: a & b (one ampersand; && is logical AND)
  • Main use: masking, e.g. x & 0xFF keeps the lowest 8 bits
  • Never bigger: for numbers of 0 or more, A AND B is at most the smaller of A and B

What Is Bitwise AND?

AND is the logic operation that is true only when both inputs are true. A bitwise AND applies it to every bit position of two binary numbers at once: bit 0 of A with bit 0 of B, bit 1 with bit 1, and so on, with no carrying between positions. In Boolean algebra it is written A · B or A ∧ B.

Because AND can only clear bits, never set them, it is the natural tool for picking out part of a number. The number you AND with is called a mask: its 1 bits mark the positions to keep, and its 0 bits mark the positions to clear.

AND Truth Table

ABA AND B
000
010
100
111

Example Calculations

12 AND 10: 0000 1100 & 0000 1010 = 0000 1000 = 8
5 AND 3: 0000 0101 & 0000 0011 = 0000 0001 = 1
200 AND 15: 1100 1000 & 0000 1111 = 0000 1000 = 8
45 AND 7: 0010 1101 & 0000 0111 = 0000 0101 = 5
−8 AND 255 (8-bit): 1111 1000 & 1111 1111 = 1111 1000 = 248 (−8 signed)

The 45 AND 7 line shows a handy shortcut: for numbers of 0 or more, x AND (2k − 1) is the remainder of x ÷ 2k, so 45 AND 7 = 45 mod 8 = 5.

Bit Masking with AND

TaskExpressionExample
Odd or evenn & 113 & 1 = 1 (odd)
Keep the low bytex & 0xFF0xDEADBEEF & 0xFF = 0xEF
Test a flagflags & (1 << k)6 & (1 << 1) = 2 (set)
Power of two?n & (n − 1) == 016 & 15 = 0 (yes)
Network addressIP & subnet mask130 & 192 = 128 (last octet)
Apply a umaskmode & ~umask0o666 & ~0o022 = 0o644

The subnet example is how routers find a network: 192.168.1.130 with mask 255.255.255.192 (/26) gives network 192.168.1.128, because only the last octet changes and 130 AND 192 = 128.

AND in Programming Languages

C, C++, Java, C#, JavaScript, Python and Go all write bitwise AND as a single & and logical AND as && (Python uses the word and). JavaScript turns both sides into 32-bit signed integers first unless you use BigInt, and Python integers have no fixed width, so −8 & 255 gives 248 there. Go also has &^ (AND NOT), which clears the bits set in the right side. To compare AND with OR, XOR, NAND, NOR and XNOR on the same numbers, use the Bitwise Calculator.

Key Terms

AND
1 only when both inputs are 1.
Mask
A number whose 1 bits mark the positions an AND should keep.
Bit width
How many bits each number has, such as 8, 16, 32 or 64.
Two’s complement
The usual way to store negative numbers: −n in w bits is stored as 2w − n.
Logical AND (&&)
A true / false test on whole values, not bit by bit.

How to Use the AND Calculator

  1. Enter A and B. Type the two numbers in the “Number A” and “Number B” boxes. Pressing Enter in A moves you to B. Decimal numbers can be negative.
  2. Pick the number format. Choose Dec, Bin, Oct or Hex for both numbers, or type a 0b, 0o or 0x prefix and the format switches by itself. Switching format converts the numbers already typed, so 12 becomes 1100 in binary.
  3. Set the bit width. Use the 8, 16, 32 or 64 chips, or type any width from 1 to 64. The width only changes the answer when a negative number is involved.
  4. Read the result. The AND appears as soon as both numbers are filled, in the same format you typed, with the working underneath (for example 12 AND 10 = 8, 1100 & 1010 = 1000). Below it, A, B and the result are lined up bit by bit, and a table gives the result in binary, hexadecimal, octal, unsigned and signed decimal.
  5. Copy the result. Press Copy to copy the result without the spaces between digit groups. Press Clear or Esc to start again.

Example: pick Hex, enter A = DEADBEEF and B = FF with a 32-bit width, and the result is 0000 00EF, the lowest byte of A.

Who Can Use This Calculator?

ProgrammersChecking masks, flags and permission bits before they go into code
Network EngineersWorking out network addresses from IPs and subnet masks
StudentsBoolean logic, truth tables and two’s complement coursework
Electronics HobbyistsChecking AND gate logic, such as the 7408 chip

Frequently Asked Questions (FAQs)

What is a bitwise AND?

A bitwise AND compares two numbers one bit position at a time and gives 1 only where both numbers have a 1. Every other position becomes 0. In most programming languages it is written with a single ampersand: a & b.

How do you calculate 12 AND 10?

Write both numbers in binary: 12 = 1100 and 10 = 1010. Compare each column and keep a 1 only where both have a 1. That happens only in the 8s column, so 12 AND 10 = 1000 = 8.

What is the AND truth table?

0 AND 0 = 0, 0 AND 1 = 0, 1 AND 0 = 0 and 1 AND 1 = 1. The output is 1 only in the last row, when both inputs are 1. It works like multiplying single bits.

What is bitwise AND used for?

AND is used for masking: keeping some bits and clearing the rest. x & 0xFF keeps the lowest 8 bits, n & 1 tells odd from even, flags & WRITE tests whether a permission bit is set, and an IP address AND its subnet mask gives the network address, for example 192.168.1.130 AND 255.255.255.192 = 192.168.1.128.

What is the difference between & and && in programming?

& is bitwise AND and works on every bit: 12 & 10 = 8. && is logical AND: it only asks whether both sides are true (non-zero) and stops early if the left side is false. In C, 12 && 10 is 1 (true), and 6 & 3 = 2 while 6 && 3 = 1.

Can the AND of two numbers be bigger than either number?

No, not for numbers of 0 or more. AND can only turn bits off, never on, so A AND B is never larger than the smaller of A and B. A AND 0 = 0, and A AND A = A.

How do you check if a number is even or a power of two with AND?

n & 1 is 0 for even numbers and 1 for odd numbers, because the lowest bit is the 1s place. n & (n − 1) is 0 only when n is a power of two (for n above 0): 16 & 15 = 0, but 12 & 11 = 8.

How does AND work with negative numbers?

Negative numbers are stored in two’s complement, so AND works on that bit pattern. −1 has every bit set, so −1 AND x = x. In 8 bits, −8 is 1111 1000, and −8 AND 255 = 1111 1000, which reads as 248 unsigned or −8 signed. This is the only case where the bit width changes the answer.

Is the AND Calculator free?

Yes. It is completely free, needs no sign-up, and can be used as many times as you like.

Try our AND Calculator to find the bitwise AND of any two numbers instantly.