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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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 multiplies an unsigned value by . A right shift (>>) moves all bits right; for an unsigned value it divides by , 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 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 AND . [2 marks]
- Cue. Keep bits set in both: .
Q3. What value results from left-shifting by 2, and what arithmetic operation is that? [2 marks]
- Cue. ; it multiplies by .
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: .
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 XOR . (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 . 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
- Convert whole numbers between binary, denary and hexadecimal, and perform binary addition, explaining the role of place value and overflow
A focused answer to the H2 Computing outcome on number bases. Place value in binary and hexadecimal, conversion methods between binary, denary and hexadecimal, binary addition, and the meaning of overflow.
- Represent signed integers using two's complement, convert to and from denary, and perform subtraction by addition, explaining range and overflow
A focused answer to the H2 Computing outcome on signed integers. Two's complement encoding, converting to and from denary, subtraction as addition, the representable range, and detecting signed overflow.
- Explain floating-point representation in terms of sign, mantissa and exponent, and discuss precision, range and rounding error
A focused answer to the H2 Computing outcome on floating point. The sign, mantissa and exponent fields, normalisation, the trade-off between precision and range, and why rounding errors arise.
- Construct and interpret logic gates and truth tables, write Boolean expressions, and simplify them using Boolean algebra laws
A focused answer to the H2 Computing outcome on digital logic. The AND, OR, NOT, NAND, NOR and XOR gates, building and reading truth tables, writing Boolean expressions, and simplifying them with Boolean algebra laws and De Morgan.
- Explain symmetric and public-key (asymmetric) encryption, digital signatures and common network threats and defences
A focused answer to the H2 Computing outcome on network security. Symmetric and public-key encryption, the key-distribution problem, digital signatures for authenticity, hashing, and common threats and defences.