Bitwise Playground
Type two numbers in decimal, hex, or binary and see every bitwise operation laid out as a bit grid. Click any bit on A or B to toggle it and watch AND, OR, XOR, NOT and the three shift variants update instantly.
Width
Results
A AND B
A & B
A OR B
A | B
A XOR B
A ^ B
NOT A
~A
A NAND B
~(A & B)
A << 2
A << n
A >>> 2
A >>> n (logical)
A >> 2
A >> n (arith)
The bitwise operators
| Operator | Rule | Use case |
|---|---|---|
| A & B | 1 if both bits are 1 | Mask bits off, check flags |
| A | B | 1 if either bit is 1 | Combine flags, set bits |
| A ^ B | 1 if bits differ | Flip bits, swap without temp |
| ~A | Flip every bit | Negate within a width |
| A << n | Shift left, fill 0 on the right | Multiply by 2n |
| A >>> n | Logical right shift, fill 0 on left | Unsigned divide by 2n |
| A >> n | Arithmetic right shift, preserves sign bit | Signed divide by 2n |
Bit tricks worth knowing
- Check bit k:
(x >> k) & 1 - Set bit k:
x | (1 << k) - Clear bit k:
x & ~(1 << k) - Toggle bit k:
x ^ (1 << k) - Lowest set bit:
x & −x— isolates the rightmost 1. - Clear lowest set bit:
x & (x − 1)— counts bits one at a time. - XOR swap:
a ^= b; b ^= a; a ^= b;swaps two integers without a temporary. - Power of two?
x !== 0 && (x & (x − 1)) === 0.
Logical vs arithmetic right shift
>>> always fills the vacated high bits with 0 — useful when you treat the number as unsigned. >> copies the original sign bit into the high bits, so negative numbers stay negative when divided by a power of two. Toggle a high bit on a wide width and slide n to see the difference between the two right-shift outputs.
Width matters
The width selector decides how many low bits are kept. At 8-bit, ~0 equals 255; at 16-bit it's 65,535; at 32-bit it's 4,294,967,295. The same conceptual bit pattern produces very different decimal numbers depending on how wide a register you imagine it living in — a common source of confusion in C/Go and in network protocol code.