Bitwise Operation Calculator
Run AND, OR, XOR, NOT and shift operations and watch every individual bit change in a colour-coded grid. Supports decimal, binary, hexadecimal and octal input at 8, 16 or 32 bits.
Photo by Umberto on Unsplash
Bitwise operation calculator
Key takeaways
- Bitwise operators apply a rule to each bit column independently, which is why they compile to single CPU instructions.
- Bit flags pack many booleans into one integer: OR sets, AND tests, AND-NOT clears and XOR toggles.
- JavaScript coerces bitwise operands to signed 32-bit integers, so negative results and wrap-around are expected rather than bugs.
The seven bitwise operators
Bitwise operators work on the individual bits of an integer rather than on its value as a whole. Each operator lines the operands up bit by bit and applies a simple rule to every column independently, which makes them extremely fast: a 32-bit AND is a single CPU instruction.
| Operator | Symbol | Rule | Example |
|---|---|---|---|
| AND | & | 1 only when both bits are 1 | 12 & 10 = 8 |
| OR | | | 1 when either bit is 1 | 12 | 10 = 14 |
| XOR | ^ | 1 when the bits differ | 12 ^ 10 = 6 |
| NOT | ~ | Flips every bit | ~12 = -13 |
| Left shift | << | Moves bits left, multiplying by 2 per position | 3 << 2 = 12 |
| Right shift | >> | Moves bits right, keeping the sign bit | -12 >> 2 = -3 |
| Unsigned right shift | >>> | Moves bits right, filling with zeros | -12 >>> 28 = 15 |
Truth table
| A | B | A AND B | A OR B | A XOR B | NOT A |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 | 0 |
Bit flags: the most common real use
Packing many booleans into one integer is the classic application. Each bit is a named flag, and the operators become set operations: OR adds a flag, AND tests it, AND-with-NOT removes it, XOR toggles it.
const READ = 1 << 0; // 0001 = 1
const WRITE = 1 << 1; // 0010 = 2
const EXECUTE = 1 << 2; // 0100 = 4
const DELETE = 1 << 3; // 1000 = 8
let perms = READ | WRITE; // 0011 = 3
const canWrite = (perms & WRITE) !== 0; // true
perms &= ~WRITE; // remove write -> 0001
perms ^= EXECUTE; // toggle execute -> 0101Unix file permissions work exactly this way: chmod 755 is three octal digits, each holding a three-bit read/write/execute triple. Linux kernel flags, TCP header flags, CSS event masks and graphics blend modes all use the same pattern.
Useful bit manipulation idioms
n & 1— test whethernis odd, faster and clearer than a modulo for hot loops.n & (n - 1)— clears the lowest set bit; repeating it until zero counts set bits (Brian Kernighan’s algorithm).n & (n - 1)) === 0— true whennis a power of two (forn > 0).a ^ b ^ b === a— XOR is its own inverse, which is why it underpins one-time pads, simple checksums and the classic swap-without-a-temporary trick.x << 1andx >> 1— multiply and divide by two. Modern compilers do this for you, so prefer the arithmetic version for readability.(x + 7) & ~7— round up to the next multiple of eight, the standard memory-alignment idiom.
JavaScript caveat. Bitwise operators coerce their operands to signed 32-bit integers, so 2**31 and above wrap around and ~5 gives -6. Use >>> 0 to read a result as unsigned, or switch to BigInt when you genuinely need more than 32 bits.
Two’s complement and negative numbers
Computers store signed integers in two’s complement. The highest bit is the sign bit: 0 means non-negative, 1 means negative. To negate a number you flip every bit and add one. In eight bits, 5 is 00000101, so −5 is 11111011.
This representation exists because it lets one adder circuit handle both addition and subtraction: a - b is simply a + (-b) with no special case, and there is only one representation of zero. It also explains the asymmetric range of signed types — an 8-bit signed integer spans −128 to 127, not −127 to 127.
Switch the bit-width selector above to 8-bit and try NOT 5 to watch this happen. For the underlying arithmetic, see the binary calculator.
Frequently Asked Questions
What is the difference between & and &&?
& is bitwise AND: it compares operands bit by bit and returns a number. && is logical AND: it evaluates truthiness and short-circuits, returning one of the operands. 6 & 3 is 2 while 6 && 3 is 3.Why does NOT 5 give -6?
Because of two's complement. Flipping all bits of 5 produces the pattern that represents −6 in signed arithmetic. The identity ~n === -(n + 1) always holds for signed integers.
Are bitwise operations faster than arithmetic?
On the CPU they are single-cycle instructions, but modern compilers already convert x * 2 to a shift where it helps. Use bitwise operators for flags and protocols, not as a micro-optimisation for ordinary maths.
What does 1 << n mean?
It creates a value with a single bit set at position n, counting from zero on the right. 1 << 3 is binary 1000, decimal 8. This is the standard way to define bit flags.
How do I count the number of set bits?
Repeatedly apply n &= n - 1 and count the iterations, or use a built-in population count where available. The calculator shows the count for every result in the “bits set” tile.
Why does the tool limit me to 32 bits?
JavaScript's bitwise operators are defined on 32-bit signed integers, so results beyond that range are not representable without BigInt. The 8- and 16-bit modes mask the result so you can reason about smaller word sizes.
Sources & further reading
- MDN: JavaScript operators — exact semantics of every bitwise operator
- Wikipedia: Two's complement — how signed integers are represented in hardware
- Bit Twiddling Hacks — Sean Eron Anderson's classic catalogue of bit manipulation tricks
- File-system permissions — the canonical real-world example of bit flags