All subjects · Computer Science · WCP02 Practical Programming and Problem-solving
Bitwise operators
Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving
Bitwise operators work on the individual bits of integers. They are used for masks, flags and fast multiplication or division by powers of 2.
The operators (spec 7.1.3)
Line the numbers up in binary and apply the rule to each column:
| Operator | Name | Rule for each bit | 12 op 10 | Result |
|---|---|---|---|---|
| & | AND | 1 only if both bits are 1 | 1100 & 1010 | 8 (1000) |
| | | OR | 1 if either bit is 1 | 1100 | 1010 | 14 (1110) |
| ^ | XOR | 1 if the bits are different | 1100 ^ 1010 | 6 (0110) |
| ~ | NOT | flips every bit; for Python integers ~x = -x - 1 | ~12 | -13 |
| << | shift left | moves bits left, filling with 0 | 12 << 2 | 48 |
| >> | shift right | moves bits right (arithmetic: keeps the sign) | 12 >> 2 | 3 |
Shifts multiply and divide
Shifting left by n multiplies by 2 to the power n. Shifting right by n is integer division by 2 to the power n (rounding down, so it works for negative numbers as an arithmetic shift).
print(5 << 3)
print(100 >> 2)
print(-20 >> 2)Output:
40
25
-5Masks
A mask picks out or changes chosen bits:
- x & mask keeps only the bits that are 1 in the mask (test or clear bits).
- x | mask sets the mask bits to 1.
- x ^ mask toggles the mask bits.
flags = 0b1011
print(flags & 0b0010 != 0)
print(bin(flags | 0b0100))
print(bin(flags ^ 0b1111))
print(7 & 1, 8 & 1)Output:
True
0b1111
0b100
1 0x & 1 is 1 for odd numbers and 0 for even numbers. bin() shows the binary form with the prefix 0b.
Worked example
Find 0b110101 & 0b001111 and say what the mask does.
- 110101
- 001111 (mask)
- AND each column: 000101
Answer: Result 5 (000101): the mask keeps the lower 4 bits and clears the rest
Check yourself
- What is displayed when this code runs?
```python
print(13 & 6)
```
Show answer
4. 1101 & 0110 = 0100, which is 4.
- What is displayed when this code runs?
```python
print(9 | 4)
```
Show answer
13. 1001 | 0100 = 1101, which is 13.
- What is displayed when this code runs?
```python
print(15 ^ 9)
```
Show answer
6. 1111 ^ 1001 = 0110, which is 6.
- What is displayed when this code runs?
```python
print(3 << 4)
```
Show answer
48. Shifting left 4 places multiplies by 16: 3 x 16 = 48.
- What is displayed when this code runs?
```python
print(90 >> 3)
```
Show answer
11. Shifting right 3 places is integer division by 8: 90 // 8 = 11.
- What is displayed when this code runs?
```python
print(~5)
```
Show answer
-6. For Python integers ~x = -x - 1, so ~5 = -6.
- This code should print 'odd' or 'even' using a bitwise test, for n = 6 and n = 7. Which line completes it?
```python
for n in [6, 7]:
__________
print("odd", end=" ")
else:
print("even", end=" ")
```
- `if n | 1 == 1:`
- `if n >> 1 == 1:`
- `if n ^ 1 == 1:`
- `if n & 1 == 1:`
Show answer
`if n & 1 == 1:`. The lowest bit of an odd number is 1, so n & 1 is 1 for odd numbers.
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Computer Science specification (XCP01/YCP01), first teaching 2026
- Pearson IAL Computer Science Unit 2 Programming Language Subset (PLS), Python 3
- Pearson IAL Computer Science IAS sample assessment materials (Unit 2 paper and mark scheme)
Last updated 8 November 2026. Spotted a mistake? Tell us.