Bitwise Calculator

Apply AND, OR, XOR, NAND, NOR or XNOR to two numbers and see the result in binary, hex, octal and decimal.

Updated

decimal
Operation
Enter to calculate Esc to clear

Bitwise Calculator

Our Bitwise Calculator applies AND, OR, XOR, NAND, NOR or XNOR to two numbers bit by bit, the same as the &, | and ^ operators in most programming languages. 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 follow each bit.

Quick answer

A bitwise operation compares two numbers one binary digit at a time. AND gives 1 where both bits are 1, OR gives 1 where either bit is 1, and XOR gives 1 where the bits differ. For 12 (1100) and 10 (1010): 12 & 10 = 8, 12 | 10 = 14 and 12 ^ 10 = 6. Enter any two numbers above and the calculator shows the result instantly.

  • AND (&): keeps or tests bits, e.g. n & 1 tells odd from even
  • OR (|): sets bits, e.g. 0xF0 | 0x0F = 0xFF
  • XOR (^): flips bits, and a ^ a = 0
  • NAND, NOR, XNOR: the same three with every bit flipped, so they depend on the bit width

What Are Bitwise Operations?

Bitwise operations treat a number as a row of bits and combine two rows column by column. Each column is handled on its own with a simple logic rule, and there is no carrying from one column to the next, so the result is always the same width as the inputs.

AND gives 1 only when both bits are 1. OR gives 1 when at least one bit is 1. XOR (exclusive or) gives 1 when exactly one bit is 1. NAND, NOR and XNOR are the same three followed by NOT, which flips every bit of the result, including the leading zeros. That is why the bit width matters for them.

Truth Table

ABANDORXORNANDNORXNOR
00000111
01011100
10011100
11110001

Example Calculations

With A = 12 (0000 1100) and B = 10 (0000 1010) in 8 bits:

AND: 0000 1100 & 0000 1010 = 0000 1000 = 8
OR: 0000 1100 | 0000 1010 = 0000 1110 = 14
XOR: 0000 1100 ^ 0000 1010 = 0000 0110 = 6
NAND: ~(0000 1000) = 1111 0111 = 247 (−9 signed)
NOR: ~(0000 1110) = 1111 0001 = 241 (−15 signed)
XNOR: ~(0000 0110) = 1111 1001 = 249 (−7 signed)

In 16 bits the last three would be 65,527, 65,521 and 65,529 instead, because eight more leading zeros get flipped to ones.

Common Bitwise Tricks

TaskExpressionExample
Odd or evenn & 113 & 1 = 1 (odd)
Keep the low 16 bitsx & 0xFFFF0xDEADBEEF & 0xFFFF = 0xBEEF
Set bit kx | (1 << k)8 | (1 << 0) = 9
Clear bit kx & ~(1 << k)15 & ~(1 << 1) = 13
Toggle bit kx ^ (1 << k)5 ^ (1 << 2) = 1
Power of two?n & (n − 1) == 016 & 15 = 0 (yes)
Combine flagsREAD | WRITE4 | 2 = 6

XOR has two handy rules: a ^ a = 0 and a ^ 0 = a. That is why XOR-ing a value with the same key twice gives the original back, and why XOR is used in checksums and simple ciphers.

Bitwise Operators in Programming Languages

LanguageANDORXORNOTNotes
C / C++ / Java / C#&|^~Width follows the type (int, long, byte...)
JavaScript&|^~Numbers become 32-bit signed integers; BigInt keeps every bit
Python&|^~No fixed width; mask with & 0xFF etc. to get one
Go&|^^xUnary ^ is NOT; &^ is AND NOT (bit clear)

None of these languages has a NAND, NOR or XNOR operator; write them as ~(a & b), ~(a | b) and ~(a ^ b). In C, Java and JavaScript, ~ flips all 32 bits of an int, so ~(12 & 10) is −9, the same as the signed 8-bit result here.

Key Terms

Bit
A single binary digit, 0 or 1.
Bit width
How many bits each number has, such as 8, 16, 32 or 64.
Mask
A number whose 1 bits pick the positions an operation should keep, set or flip.
Two’s complement
The usual way to store negative numbers: −n in w bits is stored as 2w − n.
Signed / unsigned
Two ways to read the same bits: 1111 0111 is 247 unsigned or −9 signed (8 bits).

How to Use the Bitwise 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. Choose the operation and bit width. Pick AND, OR, XOR, NAND, NOR or XNOR. Set the bit width with the 8, 16, 32 or 64 chips, or type any width from 1 to 64.
  4. Read the result. The result appears as soon as both numbers are filled, in the same format you typed, with the working underneath (for example 12 & 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: enter A = 12 and B = 10, pick XOR and an 8-bit width, and the result is 6, with 0000 1100 and 0000 1010 giving 0000 0110.

Who Can Use This Calculator?

ProgrammersChecking flags, masks and bitwise logic before they go into code
Computer Science StudentsDigital logic, truth tables and two’s complement coursework
Network and Systems EngineersSubnet masks, permission bits and register fields
Puzzle and CTF SolversQuick XOR and mask checks without writing a script

Frequently Asked Questions (FAQs)

What is a bitwise operation?

A bitwise operation works on the binary digits of numbers one position at a time. Bit 0 of A is combined with bit 0 of B, bit 1 with bit 1, and so on, and each pair gives one bit of the result. There is no carrying between positions, unlike addition.

How do you calculate 12 AND 10?

Write both numbers in binary and compare each column: 12 = 1100 and 10 = 1010. AND keeps a 1 only where both have a 1, which is only the 8s column, so 12 & 10 = 1000 = 8. With the same numbers, 12 | 10 = 1110 = 14 and 12 ^ 10 = 0110 = 6.

What is the difference between AND, OR and XOR?

AND gives 1 only when both bits are 1. OR gives 1 when at least one bit is 1. XOR (exclusive or) gives 1 when the bits are different, so 1 ^ 1 = 0. AND is used to keep or test bits, OR to set bits, and XOR to flip bits.

What are NAND, NOR and XNOR?

They are AND, OR and XOR with every result bit flipped: NAND = NOT (A AND B), NOR = NOT (A OR B) and XNOR = NOT (A XOR B). Because they flip the leading zeros too, the answer depends on the bit width. In 8 bits, 12 NAND 10 = 1111 0111 = 247, which is −9 when read as signed.

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). The same goes for | (bitwise OR) and || (logical OR).

How do you check if a number is even or odd with bitwise AND?

Use n & 1. The lowest bit is 1 for odd numbers and 0 for even numbers, so 13 & 1 = 1 (odd) and 12 & 1 = 0 (even). A related trick: n & (n − 1) is 0 only when n is a power of two, for example 16 & 15 = 0 but 12 & 11 = 8.

How do you set, clear and toggle a bit?

Build a mask with only bit k set, 1 << k, then: x | (1 << k) sets the bit, x & ~(1 << k) clears it, x ^ (1 << k) toggles it, and x & (1 << k) is non-zero when it is set. See the Bit Shift Calculator for the shift itself.

How do bitwise operations work on negative numbers?

Negative numbers are stored in two’s complement, so the operation works on that bit pattern. In 8 bits, −1 is 1111 1111, so −1 & 0xFF = 255 and −8 & 0xFF = 248. Choose the bit width that matches your data type: the same pattern reads differently at different widths.

Is the Bitwise Calculator free?

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

Try our Bitwise Calculator to apply AND, OR or XOR to any two numbers instantly.