Skip to content

Bitwise Calculator

Type two numbers (decimal, 0x hex or 0b binary) and pick an operation to see the result with every bit lined up.

Operation

Decimal, 0x hex, 0b binary or 0o octal; negative numbers are two’s complement.

Width

AND, 8 bits

0000 0101

181 & 0x0F = 5 (0x05) in 8 bits.

A
B
=
Decimal
5
Hex
0x05
Signed
5

How the bitwise operators work

Bitwise operators work on each bit position separately, with no carries between columns. AND gives 1 only where both bits are 1, OR where either is, and XOR where exactly one is. NOT flips every bit of a single number.

Shifts move the whole pattern. A left shift by n multiplies by 2ⁿ, dropping bits that fall off the top. A logical right shift (>>> in Java and JavaScript) fills from the left with zeros; an arithmetic right shift (>> on signed values) copies the sign bit, so it divides negative numbers by 2ⁿ rounding down.

The width matters for NOT and shifts: NOT 0 is 255 in 8 bits but 4,294,967,295 in 32. Pick the width your language or register uses; JavaScript’s bitwise operators work on 32-bit signed integers.

AND: 1·1 = 1 · OR: 1 if any · XOR: 1 if different · x << n = x × 2ⁿ

Common bit tricks

Patterns used for flags, masks and fast arithmetic, each with an 8-bit example.

Patterns used for flags, masks and fast arithmetic, each with an 8-bit example.
ExpressionWhat it doesExample (8 bits)
x & 1odd test: 1 if x is odd00101101 → 1
x & (x − 1)clear the lowest set bit00101100 → 00101000
x & −xkeep only the lowest set bit00101100 → 00000100
x | (1 << n)set bit n (n = 1)00101100 → 00101110
x & ~(1 << n)clear bit n (n = 2)00101100 → 00101000
x ^ (1 << n)toggle bit n (n = 0)00101100 → 00101101
(x >> n) & 1read bit n (n = 3)00101100 → 1
x & 0x0Fkeep the low nibble10101100 → 00001100
x << 3multiply by 800000101 → 00101000
x >> 2divide by 4, rounding down00101101 → 00001011
Show all 12 rows
x ^ xalways 001101001 → 00000000
~xNOT, equal to −x − 1 when signed00000101 → 11111010

Frequently Asked Questions

What is the difference between & and && ?

& is bitwise AND: it combines two numbers bit by bit (6 & 3 = 2). && is logical AND: it asks whether both values are true and returns a true/false result.

What does XOR do?

It sets a bit where the two inputs differ. XOR-ing with a mask toggles the masked bits, and XOR-ing a value with itself gives 0.

Why does NOT 5 give −6?

NOT flips every bit: 00000101 becomes 11111010, which is −6 when read as a signed (two’s complement) number and 250 when read unsigned. In general ~x = −x − 1.