Skip to main content
SingaporeComputer ScienceSyllabus dot point

How do programs manipulate individual bits, and what are masks and shifts used for?

Apply bitwise AND, OR, XOR, NOT and shift operations, and use masks to set, clear, toggle and test individual bits

A focused answer to the H2 Computing outcome on bitwise operations. The AND, OR, XOR and NOT operators, left and right shifts, and using masks to set, clear, toggle and test individual bits.

Generated by Claude Opus 4.88 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. The answer
  3. Examples in context
  4. Try this

What this dot point is asking

SEAB wants you to apply the bitwise operators AND, OR, XOR and NOT, the left and right shifts, and to use masks to set, clear, toggle and test individual bits. The key idea is that bitwise operators work on each bit position independently, which lets a single byte pack and manipulate many separate flags efficiently.

The answer

The four bitwise operators

Each operator combines two bits position by position (NOT takes one):

  • AND (&): 1 only if both bits are 1. Used to clear or isolate bits.
  • OR (|): 1 if either bit is 1. Used to set bits.
  • XOR (^): 1 only if the bits differ. Used to toggle bits.
  • NOT (~): inverts every bit. Used to build the complement of a mask.
  A:   1 1 0 0
  B:   1 0 1 0
A&B:   1 0 0 0
A|B:   1 1 1 0
A^B:   0 1 1 0
 ~A:   0 0 1 1   (within the width)

Shifts

A left shift (<<) moves all bits left, filling with 0 on the right; shifting by kk multiplies an unsigned value by 2k2^k. A right shift (>>) moves all bits right; for an unsigned value it divides by 2k2^k, discarding the remainder.

Masks

A mask is a chosen bit pattern used with an operator to act on specific positions. A single-bit mask for bit nn is 1 << n. The standard operations:

mask = 1 << n
value = value | mask        # set bit n to 1
value = value & ~mask       # clear bit n to 0
value = value ^ mask        # toggle bit n
is_set = (value & mask) != 0   # test bit n

Each operation leaves all other bits untouched, which is what makes a single byte a tidy container for up to eight independent flags.

Examples in context

Example 1. Hardware control registers. A device driver configures a peripheral by writing to a control register where each bit enables a feature. Setting bit 4 to turn on an interrupt, without disturbing the other settings, is exactly an OR with 1 << 4 - which is why embedded code is full of masks.

Example 2. File permissions. Unix permissions pack read, write and execute as bits (4, 2, 1). Granting write to an existing permission set is an OR with the write mask, and checking whether a file is executable is an AND with the execute mask tested for non-zero. The chmod number 755 is just three groups of these bit values.

Try this

Q1. Give the Python expression to toggle bit 6 of an integer x. [1 mark]

  • Cue. x = x ^ (1 << 6); XOR with the single-bit mask flips that bit.

Q2. Compute 11001100211001100_2 AND 00111100200111100_2. [2 marks]

  • Cue. Keep bits set in both: 00001100200001100_2.

Q3. What value results from left-shifting 00000101200000101_2 by 2, and what arithmetic operation is that? [2 marks]

  • Cue. 000101002=2000010100_2 = 20; it multiplies 55 by 22=42^2 = 4.

Exam-style practice questions

Practice questions written in the style of SEAB exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

Original5 marksA status byte stores eight on/off flags. Using bitwise operations on a byte, explain with Python how to (a) set bit 3 on, (b) clear bit 3, and (c) test whether bit 3 is on, where bit 0 is the least significant bit.
Show worked answer →

Build a mask with only bit 3 set: 1≪3=000010002=81 \ll 3 = 00001000_2 = 8.

mask = 1 << 3          # 00001000

# (a) set bit 3 on, leaving other bits unchanged
flags = flags | mask

# (b) clear bit 3, leaving other bits unchanged
flags = flags & ~mask  # ~mask is 11110111

# (c) test whether bit 3 is on
is_on = (flags & mask) != 0

OR with the mask forces bit 3 to 1 and leaves the rest alone. AND with the inverted mask forces bit 3 to 0 and leaves the rest alone. AND with the mask isolates bit 3, giving a non-zero result only if it was set.

Markers reward the shifted mask, OR to set, AND with the complement to clear, and AND then a non-zero test to read a bit.

Original4 marks(a) Show the result of 10110010210110010_2 XOR 00001111200001111_2. (b) State the effect of a left shift by 1 on an unsigned binary number, and what arithmetic operation it performs. (c) Give one situation where XOR with a mask is useful.
Show worked answer →

(a) XOR gives 1 only where the bits differ:

  1 0 1 1 0 0 1 0
^ 0 0 0 0 1 1 1 1
---------------
  1 0 1 1 1 1 0 1

So the result is 10111101210111101_2. The low nibble has been inverted, because XOR with 1 flips a bit and XOR with 0 leaves it.

(b) A left shift by 1 moves every bit one place left and brings in a 0 on the right. For an unsigned number this multiplies the value by 2 (provided no set bit is shifted off the top).

(c) XOR with a mask toggles the masked bits, so it is useful for flipping selected flags, or for simple reversible transforms such as a basic XOR cipher.

Markers reward the correct XOR result, that a left shift by 1 doubles the value, and a valid toggle use.

Related dot points