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.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

The operators (spec 7.1.3)

Line the numbers up in binary and apply the rule to each column:

OperatorNameRule for each bit12 op 10Result
&AND1 only if both bits are 11100 & 10108 (1000)
|OR1 if either bit is 11100 | 101014 (1110)
^XOR1 if the bits are different1100 ^ 10106 (0110)
~NOTflips every bit; for Python integers ~x = -x - 1~12-13
<<shift leftmoves bits left, filling with 012 << 248
>>shift rightmoves bits right (arithmetic: keeps the sign)12 >> 23

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
-5

Masks

A mask picks out or changes chosen 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 0

x & 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.

  1. 110101
  2. 001111 (mask)
  3. AND each column: 000101

Answer: Result 5 (000101): the mask keeps the lower 4 bits and clears the rest

Key idea: & keeps bits set in both, | sets bits, ^ toggles bits, ~x = -x - 1, x << n multiplies by 2 to the power n, x >> n integer-divides by 2 to the power n.

Check yourself

  1. What is displayed when this code runs? ```python print(13 & 6) ```
    Show answer

    4. 1101 & 0110 = 0100, which is 4.

  2. What is displayed when this code runs? ```python print(9 | 4) ```
    Show answer

    13. 1001 | 0100 = 1101, which is 13.

  3. What is displayed when this code runs? ```python print(15 ^ 9) ```
    Show answer

    6. 1111 ^ 1001 = 0110, which is 6.

  4. 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.

  5. 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.

  6. What is displayed when this code runs? ```python print(~5) ```
    Show answer

    -6. For Python integers ~x = -x - 1, so ~5 = -6.

  7. 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.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.