Bitwise Calculator
Type two numbers (decimal, 0x hex or 0b binary) and pick an operation to see the result with every bit lined up.
Decimal, 0x hex, 0b binary or 0o octal; negative numbers are two’s complement.
AND, 8 bits
0000 0101
181 & 0x0F = 5 (0x05) in 8 bits.
- 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ⁿ
- JavaScript converts the operands of &, |, ^, ~, << and >> to 32-bit signed integers before operating on them. Source: ECMAScript, ToInt32 (bitwise operators).
- In Java, >> is an arithmetic shift that copies the sign bit, and >>> is a logical shift that fills with zeros. Source: Java Language Specification §15.19, shift operators.
Common bit tricks
Patterns used for flags, masks and fast arithmetic, each with an 8-bit example.
| Expression | What it does | Example (8 bits) |
|---|---|---|
| x & 1 | odd test: 1 if x is odd | 00101101 → 1 |
| x & (x − 1) | clear the lowest set bit | 00101100 → 00101000 |
| x & −x | keep only the lowest set bit | 00101100 → 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) & 1 | read bit n (n = 3) | 00101100 → 1 |
| x & 0x0F | keep the low nibble | 10101100 → 00001100 |
| x << 3 | multiply by 8 | 00000101 → 00101000 |
| x >> 2 | divide by 4, rounding down | 00101101 → 00001011 |
Show all 12 rowsShow fewer
| x ^ x | always 0 | 01101001 → 00000000 |
| ~x | NOT, equal to −x − 1 when signed | 00000101 → 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.