Bitwise operations apply logical rules (AND, OR, XOR, NOT) independently to each pair of corresponding bits in two binary numbers, producing a new binary result. Unlike arithmetic addition, which carries between columns, bitwise operations treat each bit as a completely separate yes/no decision.
What is the core analogy for bitwise operations?
Imagine two light switches in parallel on a circuit. Whether the bulb lights up depends on the switch settings — and the rule you apply changes the result. If the rule is "light on only when BOTH switches are on" — that is AND. If "light on when AT LEAST ONE switch is on" — that is OR. If "light on only when EXACTLY ONE switch is on" — that is XOR. NOT simply flips one switch. Bitwise operations are exactly these rules applied to every pair of bits simultaneously.
What are the four bitwise operators?
| Operator | Symbol (Python) | Rule applied to each bit pair |
|---|---|---|
| AND | & |
Output is 1 only if BOTH bits are 1 |
| OR | | |
Output is 1 if AT LEAST ONE bit is 1 |
| XOR (exclusive OR) | ^ |
Output is 1 only if the bits are DIFFERENT |
| NOT | ~ |
Output is the OPPOSITE of each single bit |
Bit-level truth tables:
| 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 |
How do bitwise operations work on binary numbers?
Worked example — 8-bit AND:
0 1 0 1 1 0 1 0 (= 90)
& 0 0 1 1 1 1 0 0 (= 60)
---------------
0 0 0 1 1 0 0 0 (= 24)
Each column is calculated independently using the AND rule. Column by column: 0&0=0, 1&0=0, 0&1=0, 1&1=1, 1&1=1, 0&1=0, 1&0=0, 0&0=0. Result: 00011000 = 24.
Worked example — 8-bit OR:
0 1 0 1 1 0 1 0 (= 90)
| 0 0 1 1 1 1 0 0 (= 60)
---------------
0 1 1 1 1 1 1 0 (= 126)
Worked example — 8-bit XOR:
0 1 0 1 1 0 1 0 (= 90)
^ 0 0 1 1 1 1 0 0 (= 60)
---------------
0 1 1 0 0 1 1 0 (= 102)
XOR outputs 1 where the bits differ and 0 where they match.
What is a bit mask and how does AND use it?
A bit mask is a binary pattern used with AND (or OR) to selectively extract or set individual bits. The mask "hides" unwanted bits by ANDing them with 0 (forcing them to 0) and "reveals" wanted bits by ANDing them with 1 (preserving their value).
Example — extracting the lower 4 bits of a byte (checking a nibble):
Value: 1 0 1 1 0 1 1 0 (= 182)
Mask: 0 0 0 0 1 1 1 1 (= 15)
AND: 0 0 0 0 0 1 1 0 (= 6)
The upper 4 bits are zeroed; the lower 4 are preserved. This technique is used in networking (extracting parts of an IP address), graphics (reading individual colour channels), and embedded systems (reading sensor flags).
What is XOR used for in computing?
XOR has a special mathematical property: XOR-ing any value with itself gives zero, and XOR is its own inverse. This makes it useful in:
- Simple error detection: If the XOR of all transmitted bits is 0, transmission was likely correct.
- Encryption: XOR-ing data with a key produces ciphertext; XOR-ing the ciphertext with the same key recovers the original (one-time pad encryption).
- Swap without a third variable:
a = a XOR b; b = a XOR b; a = a XOR b;swapsaandbwithout needing a temporary variable — a classic trick.
XOR swap trace:
| Step | a | b | Operation |
|---|---|---|---|
| Start | 5 (0101) | 3 (0011) | |
| a = a XOR b | 0110 = 6 | 3 | |
| b = a XOR b | 6 | 0101 = 5 | b now holds original a |
| a = a XOR b | 0011 = 3 | 5 | a now holds original b |
| Result | 3 | 5 | Values swapped ✓ |
What is the difference between bitwise and boolean operations?
| Feature | Bitwise | Boolean (logical) |
|---|---|---|
| Operates on | Each individual bit of an integer | Entire value treated as True/False |
| Python operators | &, |, ^, ~ |
and, or, not |
| Input type | Integers | Boolean values (True/False) |
| Short-circuits? | No | Yes (and stops at first False) |
| Example | 5 & 3 = 1 |
True and False = False |
In Python, 5 & 3 works bit-by-bit on the binary representations; 5 and 3 evaluates 5 as truthy and returns 3. They produce different results and have different purposes.
Frequently asked questions
How are bit shifts related to bitwise operations?
Bit shifts move all bits left or right by a given number of positions. A left shift by 1 (<< 1) multiplies a number by 2; a right shift (>> 1) divides by 2 (integer division). For example, 5 (00000101) left-shifted by 2 becomes 20 (00010100). Bit shifts are not covered in the KS3 curriculum but build naturally on bitwise knowledge and appear at GCSE and A-level.
Why do some programming languages use & for AND while others use AND?
It is a language design choice. Languages like Python and Java use & (and && for boolean short-circuit AND) to distinguish bitwise from logical operations. Languages like Python's pseudocode context, VB.NET, and SQL use spelled-out AND / OR for logical operations. Always check which context you are in — in an exam's pseudocode, AND and OR refer to boolean logic, not bitwise operations, unless the question explicitly says otherwise.
Can bitwise operations be used on negative numbers?
Yes, but the result depends on how negative numbers are represented (two's complement). NOT 5 in Python gives -6 because Python uses two's complement for integers, making ~5 = -(5+1) = -6. At KS3, bitwise operations are typically applied to positive (unsigned) binary values to avoid this complexity.
Is XOR used in real encryption?
The one-time pad — which XORs each bit of a message with a truly random key of the same length — is theoretically unbreakable when the key is random, secret, and never reused. However, it is impractical for most uses because the key must be as long as the message and securely distributed. Real-world encryption (AES, RSA) uses far more complex mathematics, though XOR remains one building block within many modern cipher algorithms.
Master bitwise operations and binary thinking with Professor Turing's step-by-step guidance at aitutors.me.