Operating on the bits, not the numbers
Bitwise operators treat a number as a sequence of individual binary digits and act on each bit position independently, which is a completely different operation from the arithmetic you normally do with numbers. This calculator runs AND, OR, XOR, NOT, and left and right shift on two numbers, showing the binary and hexadecimal result alongside the decimal one — the binary view is the part that actually explains what happened.
The operators
| Operator | Rule per bit | Common use |
|---|---|---|
| AND (&) | 1 only if both bits are 1 | Masking — isolating specific bits, like checking permission flags |
| OR (|) | 1 if either bit is 1 | Combining flags — setting one or more bits without disturbing others |
| XOR (^) | 1 if the bits differ | Toggling bits, simple checksums, and the classic swap-two-values-without-a-temp trick |
| NOT (~) | Flips every bit | Bitwise inversion — note this also flips the sign bit for signed integers |
| Left shift (<<) | Shifts bits left, filling with 0 | Multiplying by a power of two: n << 1 equals n × 2 |
| Right shift (>>) | Shifts bits right | Dividing by a power of two: n >> 1 equals n ÷ 2, rounded down |
A worked example: AND as a mask
12 & 10 — 12 is 1100 in binary, 10 is 1010. Comparing bit by bit: only the leftmost bit is 1 in both, so the result is 1000, which is 8. This exact pattern — AND against a specific bit pattern — is how permission systems commonly check "is this one specific flag set" without caring about any of the other bits.
Where bitwise operations actually show up
- Permission and flag systems. Unix file permissions, CSS media query internals, and many configuration systems pack several true/false flags into one integer, combined with OR and checked with AND.
- Networking. Subnet masks are literally a bitwise AND applied to an IP address to find its network portion.
- Graphics and colour. Packing RGB or RGBA values into a single 32-bit integer, and unpacking them, is done entirely with shifts and AND masks.
- Fast arithmetic. Shifting is a cheap way to multiply or divide by powers of two, occasionally still used in performance-sensitive code.
- Hash functions and checksums. XOR in particular is a building block in many simple hashing and error-detection schemes.