AS Level 9618 · Paper 1 · 4.3
Bit manipulation
Shifting and masking bits, and using them to monitor and control a device. Read the notes, try the past-paper questions, then check the mark scheme.
Past papers from June 2021 to June 202629 questions · 89 marks
In this subtopic
- Logical, arithmetic and cyclic shifts
- AND, OR and XOR masks to test, set, clear and toggle bits
- Using bit manipulation to monitor and control a device
4.3 · 1 of 11Book page Contents
Logical bitwise operations
In high-level programming languages, programmers rarely work directly with binary strings. Bit manipulation lets a programmer work with sets of bits using logical bitwise operations. These operations are directly supported by the processor, which makes them fast and very efficient.
The logical bitwise operators AND, OR, XOR and NOT can be used with a mask to pick out or change specific bits. Binary shifts (logical, arithmetic and cyclic) can be used to move the bits, for example within a register.
A logical bitwise operator works on a single bit (or a set of bits) within a binary string. Bitwise operations are often used in low-level programs to change the contents of registers or memory locations.
For example, in a monitoring and control system, a single 8-bit register or memory location could hold the inputs from eight sensors, or control eight actuators. Logical bitwise operators can be used with a mask to check, set, clear or toggle (flip) each bit, or set of bits, in the string.
Practise this: past-paper questions
4.3 · 2 of 11Book page Contents
Masks
Builds on
A mask is a binary string that is used with a logical bitwise operator to pick out or change a single bit (or set of bits) in another binary string.
For example, each bit in a processor’s status register has its own meaning. Suppose the first bit shows whether the result of an arithmetic operation was zero; this is called the zero flag. That bit can be examined, set or reset using a mask of the same length with a 1 in the position of the zero flag and 0 everywhere else. Here the mask would be 10000000, because the zero flag is in the first position.
Practise this: past-paper questions
- June 2021 · Paper 12 · Q4(b)(ii)An assembly language program needs to test if bit number 2 in memory address 300 is a 1. Complete the assembly language instruction to perform…
- June 2021 · Paper 12 · Q4(b)(iii)An assembly language program needs to set bit numbers 4, 5, 6 and 7 to 0, but keep bits 0 to 3 with their existing values. Write the assembly…
- November 2024 · Paper 12 · Q8(b)(ii)Explain how bit manipulation can be used to test whether the binary number stored in the ACC represents an odd denary number. Write the bit…
- November 2024 · Paper 13 · Q7(b)The instruction set also includes these bit manipulation instructions: Opcode | Operand | Explanation AND | #n/Bn/&n | Bitwise AND operation of…
Show 3 more
- June 2025 · Paper 11 · Q8(b)(i)Write the bit manipulation instruction that can be used to set the least significant bit to 1 in an 8‑bit register. All other bits must remain…
- June 2025 · Paper 13 · Q5(b)The table shows part of the instruction set for a processor. The processor has one register: the Accumulator (ACC). Opcode | Operand | Explanation…
- June 2026 · Paper 11 · Q3(b)(i)Write the bit manipulation instruction that will set the ACC to &0
4.3 · 3 of 11Book pages Contents
AND: checking and clearing bits
The logical bitwise AND operation takes two binary strings of the same length and ANDs each pair of matching bits. The result in each position is 1 if both bits are 1; otherwise it is 0.
AND can be used to find out whether a particular bit (or group of bits) is set (1) or clear (0). It can also be used to clear selected bits, that is, to set them to 0.
Consider a control system that uses an 8-bit register to control eight lights. If the bit for a light is 1, the light is on; if it is 0, the light is off. This register shows that lights 1, 4, 5 and 8 are on and lights 2, 3, 6 and 7 are off:
| Light number | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Register | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 |
Lights 3 and 4 are security lights outside the building. To pick out the bits that control these two outside lights, the mask 00110000 is used:
| Light number | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Mask | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 |
Checking selected bits. To check whether either of the outside lights is on, AND the register with the mask. If the result is not zero (00000000), at least one of those lights is on.
| Light number | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Register | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 |
| Mask | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 |
| AND | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
The result is not zero, so at least one outside light is on (light 4).
Clearing selected bits. To turn the outside lights off, AND is used again, but now the mask must have 0 in the positions to clear and 1 everywhere else. So first apply a bitwise NOT to the mask to flip its bits:
| Light number | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Mask | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 |
| NOT | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 1 |
Then AND the register with the inverted mask:
| Light number | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Register | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 |
| Inverted mask | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 1 |
| AND | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 1 |
Only the bit in position 4 (light 4) has changed. Light 3 was already off, so bit 3 did not need to change. All the other bits keep their original values.
Practise this: past-paper questions
- June 2021 · Paper 12 · Q4(b)(ii)An assembly language program needs to test if bit number 2 in memory address 300 is a 1. Complete the assembly language instruction to perform…
- June 2021 · Paper 12 · Q4(b)(iii)An assembly language program needs to set bit numbers 4, 5, 6 and 7 to 0, but keep bits 0 to 3 with their existing values. Write the assembly…
- November 2021 · Paper 11 · Q6(b)The following table shows part of the instruction set for a processor. The processor has one general purpose register, the Accumulator (ACC), and…
- November 2021 · Paper 12 · Q8(b)(i)In the following table, each row shows the current contents of the ACC in binary and the instruction that will be performed on those contents.…
Show 16 more
- June 2022 · Paper 11 · Q6(c)(i)Complete the register to show the result after the instruction AND #2 is executed. Register before: 0 1 1 0 1 1 0 1 Register after
- June 2022 · Paper 13 · Q3(c)The table shows part of the instruction set for a processor. The processor has one general purpose register, the Accumulator (ACC). | Opcode |…
- November 2022 · Paper 11 · Q6(b)(ii)The current contents of the ACC are: 1 0 0 1 0 0 1 1 Show the result after the execution of the following instruction. AND B11110000
- November 2022 · Paper 12 · Q7(b)(ii)The current contents of the ACC are: 0 1 0 1 0 0 1 1 Show the contents of the ACC after the execution of the following instruction. AND 50
- November 2022 · Paper 13 · Q6(b)(i)The current contents of the ACC are: 0 0 1 1 0 1 1 0 Show the contents of the ACC after the execution of the following instruction. AND B01001100
- November 2023 · Paper 11 · Q8(c)(i)The current contents of the ACC are: 0 1 0 0 1 1 1 1 Show the contents of the ACC after the execution of the following instruction. AND B10100101
- June 2024 · Paper 11 · Q4(b)The processor includes these bit manipulation instructions: Opcode | Operand | Explanation AND | #n/Bn/&n | Bitwise AND operation of the contents…
- June 2024 · Paper 12 · Q5(b)The processor includes these bit manipulation instructions: Opcode | Operand | Explanation AND | #n/Bn/&n | Bitwise AND operation of the contents…
- June 2024 · Paper 13 · Q3(b)The processor includes these bit manipulation instructions: | Opcode | Operand | Explanation | | AND | #n/Bn/&n | Bitwise AND operation of the…
- November 2024 · Paper 12 · Q8(b)(i)The table has three sets of instructions. The binary number 10011010 is reloaded into the ACC before each set of instructions is run. Complete the…
- November 2024 · Paper 12 · Q8(b)(ii)Explain how bit manipulation can be used to test whether the binary number stored in the ACC represents an odd denary number. Write the bit…
- November 2024 · Paper 13 · Q7(b)The instruction set also includes these bit manipulation instructions: Opcode | Operand | Explanation AND | #n/Bn/&n | Bitwise AND operation of…
- June 2025 · Paper 13 · Q5(b)The table shows part of the instruction set for a processor. The processor has one register: the Accumulator (ACC). Opcode | Operand | Explanation…
- November 2025 · Paper 11 · Q4(c)The ACC currently contains the following positive binary integer: 1 1 1 0 0 0 1 1 Write the contents of the ACC after the instruction AND #63 is…
- November 2025 · Paper 13 · Q6(a)Complete the table by writing the contents of the ACC after the execution of each instruction. Current contents of the ACC | Instruction |…
- June 2026 · Paper 11 · Q3(b)(i)Write the bit manipulation instruction that will set the ACC to &0
4.3 · 4 of 11Book page Contents
OR: setting bits
Builds on
The logical bitwise OR operation takes two binary strings of the same length and ORs each pair of matching bits. The result in each position is 1 if either or both bits are 1; if both bits are 0, the result is 0.
AND with a mask is used to check or clear bits. OR with a mask is used to set one or more bits to 1.
To set (turn on) the outside lights, use the same register, the same mask (00110000) and OR:
| Light number | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Register | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 |
| Mask | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 |
| OR | 1 | 0 | 1 | 1 | 1 | 0 | 0 | 1 |
The bit in position 3 (light 3) has been set to 1; it was 0. All the other bits are unchanged. Light 4 was already on, so bit 4 did not need to change.
Practise this: past-paper questions
- November 2021 · Paper 11 · Q6(b)The following table shows part of the instruction set for a processor. The processor has one general purpose register, the Accumulator (ACC), and…
- November 2021 · Paper 12 · Q8(b)(i)In the following table, each row shows the current contents of the ACC in binary and the instruction that will be performed on those contents.…
- June 2022 · Paper 11 · Q6(c)(ii)Complete the register to show the result after the instruction OR #8 is executed. Register before: 0 1 1 0 1 1 0 1 Register after
- June 2022 · Paper 12 · Q3(a)The ACC currently contains the following positive binary integer: 0 1 1 0 0 1 0 1 Write the bit manipulation instruction that would change the…
Show 11 more
- June 2022 · Paper 13 · Q3(c)The table shows part of the instruction set for a processor. The processor has one general purpose register, the Accumulator (ACC). | Opcode |…
- November 2022 · Paper 11 · Q6(b)(iii)The current contents of the ACC are: 1 0 0 1 0 0 1 1 Show the result after the execution of the following instruction. OR B11001100
- November 2022 · Paper 12 · Q7(b)(iv)The current contents of the ACC are: 0 1 0 1 0 0 1 1 Show the contents of the ACC after the execution of the following instruction. OR 51
- November 2022 · Paper 13 · Q6(b)(ii)The current contents of the ACC are: 1 0 0 1 0 1 0 1 Show the contents of the ACC after the execution of the following instruction. OR B01001111
- June 2024 · Paper 11 · Q4(b)The processor includes these bit manipulation instructions: Opcode | Operand | Explanation AND | #n/Bn/&n | Bitwise AND operation of the contents…
- June 2024 · Paper 12 · Q5(b)The processor includes these bit manipulation instructions: Opcode | Operand | Explanation AND | #n/Bn/&n | Bitwise AND operation of the contents…
- November 2024 · Paper 12 · Q8(b)(i)The table has three sets of instructions. The binary number 10011010 is reloaded into the ACC before each set of instructions is run. Complete the…
- June 2025 · Paper 11 · Q8(b)(i)Write the bit manipulation instruction that can be used to set the least significant bit to 1 in an 8‑bit register. All other bits must remain…
- June 2025 · Paper 12 · Q7(b)(i)The ACC currently contains the following binary value. 1 1 1 1 0 0 0 0 Write the result after the instruction OR B00001111 is run
- June 2025 · Paper 13 · Q5(b)The table shows part of the instruction set for a processor. The processor has one register: the Accumulator (ACC). Opcode | Operand | Explanation…
- November 2025 · Paper 11 · Q4(d)The ACC currently contains the following positive binary integer: 1 1 1 0 0 0 1 1 The current contents of memory are: Address | Data 98 | 00100100…
Used later in
4.3 · 5 of 11Book pages Contents
XOR: toggling bits and clearing a register
The logical bitwise XOR operation takes two binary strings of the same length and XORs each pair of matching bits. The result in each position is 1 if exactly one of the two bits is 1; if both bits are 0, or both are 1, the result is 0.
XOR with a mask is used to toggle bits: a bit that is 1 becomes 0, and a bit that is 0 becomes 1.
To toggle the outside lights, use the same register, the same mask and XOR:
| Light number | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Register | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 |
| Mask | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 |
| XOR | 1 | 0 | 1 | 0 | 1 | 0 | 0 | 1 |
The bits in positions 3 and 4 have been toggled (flipped). All the other bits are unchanged.
Clearing all the bits. XOR is also an efficient way to set a register to zero. XOR of two bits that are the same gives 0, so XOR the register with a copy of itself:
| Light number | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Register | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 |
| Mask (copy of the register) | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 |
| XOR | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
This usually needs fewer clock cycles and less memory than loading the value zero and saving it to the register.
Practise this: past-paper questions
- November 2021 · Paper 11 · Q6(b)The following table shows part of the instruction set for a processor. The processor has one general purpose register, the Accumulator (ACC), and…
- November 2021 · Paper 12 · Q8(b)(i)In the following table, each row shows the current contents of the ACC in binary and the instruction that will be performed on those contents.…
- June 2022 · Paper 12 · Q3(b)The ACC currently contains the following positive binary integer: 0 1 1 0 0 1 0 1 Write the bit manipulation instruction that would change the…
- June 2022 · Paper 13 · Q3(c)The table shows part of the instruction set for a processor. The processor has one general purpose register, the Accumulator (ACC). | Opcode |…
Show 10 more
- November 2022 · Paper 11 · Q6(b)(i)The current contents of the ACC are: 1 0 0 1 0 0 1 1 Show the result after the execution of the following instruction. XOR B00011111
- November 2022 · Paper 12 · Q7(b)(i)The current contents of the ACC are: 0 1 0 1 0 0 1 1 Show the contents of the ACC after the execution of the following instruction. XOR B00011111
- November 2023 · Paper 11 · Q8(c)(iii)The current contents of the ACC are: 1 1 1 1 0 1 1 1 Show the contents of the ACC after the execution of the following instruction. XOR B00100101
- June 2024 · Paper 11 · Q4(b)The processor includes these bit manipulation instructions: Opcode | Operand | Explanation AND | #n/Bn/&n | Bitwise AND operation of the contents…
- June 2024 · Paper 12 · Q5(b)The processor includes these bit manipulation instructions: Opcode | Operand | Explanation AND | #n/Bn/&n | Bitwise AND operation of the contents…
- June 2024 · Paper 13 · Q3(b)The processor includes these bit manipulation instructions: | Opcode | Operand | Explanation | | AND | #n/Bn/&n | Bitwise AND operation of the…
- June 2025 · Paper 11 · Q8(b)(ii)The ACC currently contains the following binary value. 0 1 0 1 0 1 0 1 Write the result after the instruction XOR &FE is run
- June 2025 · Paper 12 · Q7(b)(ii)The ACC currently contains the following binary value. 0 0 0 1 1 1 0 1 Write the result after the instruction XOR #30 is run
- November 2025 · Paper 11 · Q4(b)The ACC currently contains the following positive binary integer: 1 1 1 0 0 0 1 1 Write the contents of the ACC after the instruction XOR &12 is…
- November 2025 · Paper 13 · Q6(a)Complete the table by writing the contents of the ACC after the execution of each instruction. Current contents of the ACC | Instruction |…
Used later in
4.3 · 6 of 11Book page Contents
NOT: flipping every bit
Builds on
The logical bitwise NOT operation flips every bit in a binary string: bits that are 0 become 1, and bits that are 1 become 0. For example:
| Bit | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Binary string | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 |
| NOT | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 0 |
Note There is no NOT instruction in the Cambridge instruction set. To flip every bit of the ACC, use XOR B11111111 (XOR with all 1s). To flip only some bits, XOR with a mask that has 1s in those positions.
4.3 · 7 of 11Book pages Contents
Logical shifts
Builds on
A logical left shift moves all the bits in a binary string a given number of places to the left. A left shift by one place means:
- moving all the bits one place to the left
- discarding the most significant (leftmost) bit
- putting a 0 into the empty place on the right
If the string represents a number, each shift one place left multiplies the number by 2. So shifting three places left multiplies the number by 23 = 8. For example, to multiply 14 by 8, shift its binary value left three times to get 112:
| Denary | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| 14 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 |
| 28 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |
| 56 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 0 |
| 112 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
In general, shifting a binary number left by n places multiplies it by 2n.
A logical right shift moves all the bits a given number of places to the right. A right shift by one place means:
- moving all the bits one place to the right
- discarding the least significant (rightmost) bit
- putting a 0 into the empty place on the left
If the string represents a number, each shift one place right divides the number by 2. In general, shifting a binary number right by n places divides it by 2n. For example, shifting 01110000 (112) right three places gives 00001110 (14), and 112 ÷ 8 = 14.
Note Bits shifted out at the end are lost. If a 1 is shifted out, the answer is no longer exact: a left shift has overflowed, or a right shift has lost the remainder (for example 7 shifted right one place gives 3).
In the Cambridge instruction set, LSL #n shifts the bits in the ACC n places to the left and LSR #n shifts them n places to the right; zeros are introduced at the other end.
Practise this: past-paper questions
- June 2021 · Paper 11 · Q3(c)(i)The Accumulator currently contains the binary number: 0 0 1 1 0 1 0 1 Write the contents of the Accumulator after the processor has executed the…
- June 2021 · Paper 11 · Q3(c)(ii)The Accumulator currently contains the binary number: 0 0 1 1 0 1 0 1 Identify the mathematical operation that the following instruction will…
- November 2021 · Paper 11 · Q6(b)The following table shows part of the instruction set for a processor. The processor has one general purpose register, the Accumulator (ACC), and…
- November 2021 · Paper 12 · Q8(b)(i)In the following table, each row shows the current contents of the ACC in binary and the instruction that will be performed on those contents.…
Show 16 more
- June 2022 · Paper 11 · Q6(c)(iii)Complete the register to show the result after the operation LSL #4 is executed. Register before: 0 1 1 0 1 1 0 1 Register after
- June 2022 · Paper 12 · Q3(d)A three-place logical shift to the left is performed on the following positive binary integer. Show the result of this logical shift. 01111110
- November 2022 · Paper 11 · Q6(b)(iv)The current contents of the ACC are: 1 0 0 1 0 0 1 1 Show the result after the execution of the following instruction. LSR #2
- November 2022 · Paper 12 · Q7(b)(iii)The current contents of the ACC are: 0 1 0 1 0 0 1 1 Show the contents of the ACC after the execution of the following instruction. LSL #3
- November 2022 · Paper 13 · Q6(b)(iii)The current contents of the ACC are: 1 0 0 1 1 1 0 1 Show the contents of the ACC after the execution of the following instruction. LSR #2
- June 2023 · Paper 11 · Q3(d)(vi)Show the result of a 3-place right logical shift on the binary number: 11001100
- June 2023 · Paper 12 · Q4(d)Show the result of a 2-place left logical shift on the binary number: 01001111
- November 2023 · Paper 11 · Q8(c)(ii)The current contents of the ACC are: 0 0 0 1 0 1 1 1 Show the contents of the ACC after the execution of the following instruction. LSR #3
- June 2024 · Paper 13 · Q3(b)The processor includes these bit manipulation instructions: | Opcode | Operand | Explanation | | AND | #n/Bn/&n | Bitwise AND operation of the…
- November 2024 · Paper 12 · Q8(b)(i)The table has three sets of instructions. The binary number 10011010 is reloaded into the ACC before each set of instructions is run. Complete the…
- June 2025 · Paper 11 · Q8(b)(iii)The ACC currently contains the following binary value. 0 1 1 0 1 0 1 1 Write the result after the instruction LSR #5 is run
- June 2025 · Paper 13 · Q7(a)(i)Show the result of a logical left shift of 2 places on the two’s complement binary integer 11001010
- November 2025 · Paper 11 · Q4(a)The ACC currently contains the following positive binary integer: 0 0 0 1 1 1 1 0 Write a bit manipulation instruction that uses a binary shift to…
- June 2026 · Paper 11 · Q3(b)(ii)The ACC currently contains the following binary value. 0 | 0 | 1 | 1 | 0 | 1 | 0 | 1 Write the result after the instruction LSR #3 is run
- June 2026 · Paper 12 · Q5(b)Complete the table by writing the answer to each statement. statement | answer The smallest 12-bit two's complement binary number The denary value…
- June 2026 · Paper 13 · Q7(c)Identify one similarity and one difference between a right logical shift and a right arithmetic shift. similarity difference
4.3 · 8 of 11Book pages Contents
Arithmetic shifts for signed numbers
Builds on
Logical shifts cannot be used to multiply or divide signed numbers, because the sign bit must not simply be shifted like the other bits. If a number is signed, an arithmetic shift must be used.
An arithmetic shift is used when the binary string represents a signed number, stored with the sign as the most significant bit (for example, in two’s complement). It makes sure that shifting does not change the sign by accident: the sign of the number is preserved.
- In an arithmetic right shift, the bits move right and the sign bit is copied into the empty place on the left, so a negative number stays negative. Each place divides the number by 2.
- In an arithmetic left shift, the bits move left and 0s are put in on the right, just as in a logical left shift. Each place multiplies the number by 2, as long as the sign bit does not change. If it would change, the result is too large to store: overflow.
Worked example
The 8-bit two’s complement integer 11001000 represents −56. Shift it one place to the right, first arithmetically, then logically.
- Step 1Arithmetic right shift: move every bit one place right and copy the sign bit (1) into the left-hand end. The answer is −28, which is −56 ÷ 2.
11001000 → 11100100 (−28) - Step 2Logical right shift: a 0 comes in on the left, so the sign bit changes and the answer is wrong.
11001000 → 01100100 (+100) - Step 3So an arithmetic shift must be used for signed numbers.
Right logical shift compared with right arithmetic shift
- Similarity: both move every bit to the right, and the least significant bit is lost.
- Difference: a logical shift puts 0s in at the left; an arithmetic shift copies the sign bit into the left, so the sign is preserved.
Practise this: past-paper questions
- November 2024 · Paper 13 · Q8(c)Describe the difference between a right logical binary shift and a right arithmetic binary shift
- June 2025 · Paper 13 · Q7(a)(i)Show the result of a logical left shift of 2 places on the two’s complement binary integer 11001010
- June 2025 · Paper 13 · Q7(a)(ii)Show the result of an arithmetic right shift of 3 places on the two’s complement binary integer 10011110
- November 2025 · Paper 13 · Q2(c)A three-place arithmetic shift to the right is performed on the following two’s complement negative integer. Show the result of this arithmetic…
4.3 · 9 of 11Book page Contents
Cyclic shifts
In a cyclic shift, no bits are lost: the bits that are shifted out at one end are moved into the other end of the register.
Worked example
Show the result of a cyclic left shift and a cyclic right shift of one place on 10110001.
- Step 1Cyclic left shift: every bit moves one place left, and the leftmost bit (1) moves into the right-hand end.
10110001 → 01100011 - Step 2Cyclic right shift: every bit moves one place right, and the rightmost bit (1) moves into the left-hand end.
10110001 → 11011000 - Step 3A cyclic shift of 8 places on an 8-bit register gives back the original value, because every bit has gone all the way round.
4.3 · 10 of 11Contents
Bit manipulation instructions
Builds on
The syllabus adds these instructions to the instruction set in 4.2. Each row gives the opcode, the operand and what the instruction does:
| Opcode | Operand | Explanation |
|---|---|---|
| AND | #n / Bn / &n | Bitwise AND operation of the contents of ACC with the operand |
| AND | <address> | Bitwise AND operation of the contents of ACC with the contents of <address> |
| XOR | #n / Bn / &n | Bitwise XOR operation of the contents of ACC with the operand |
| XOR | <address> | Bitwise XOR operation of the contents of ACC with the contents of <address> |
| OR | #n / Bn / &n | Bitwise OR operation of the contents of ACC with the operand |
| OR | <address> | Bitwise OR operation of the contents of ACC with the contents of <address> |
| LSL | #n | Bits in ACC are shifted logically n places to the left. Zeros are introduced on the right hand end |
| LSR | #n | Bits in ACC are shifted logically n places to the right. Zeros are introduced on the left hand end |
| Label | Opcode | Operand | Explanation |
|---|---|---|---|
| <label>: | <opcode> | <operand> | Labels an instruction |
| <label>: | <data> | Gives a symbolic address <label> to the memory location with contents <data> |
The same rules apply as in 4.2: only one general purpose register (ACC), IX is the Index Register, <address> can be absolute or symbolic, and #123 is denary, B01001010 is binary and &4A is hexadecimal. Watch the difference: AND #15 uses the number 15, but AND 15 uses the contents of address 15.
Exam questions usually number the bits of a byte from 7 (the most significant bit, on the left, worth 128) down to 0 (the least significant bit, on the right, worth 1). Bit n is worth 2n, so a mask for bit 2 is 00000100, which is #4.
| Bit number | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
Worked example: test a bit
The value in the memory location labelled Sensors is 10110110. Test whether bit 2 is 1, and jump to Alarm if it is.
- Step 1Load the value and AND it with a mask that has a 1 only in bit 2. Every other bit becomes 0.
LDD Sensors // 10110110 AND #4 // 00000100 - Step 2If bit 2 was 1, the ACC now holds 4; if it was 0, the ACC holds 0. Compare and jump.
CMP #4 JPE Alarm // True: jumps
Masks to remember (8-bit ACC)
- Test a bit:
ANDwith a mask that has 1 in that bit, thenCMP. For example,AND #1keeps only the least significant bit, which is 1 for an odd number and 0 for an even number. - Set bits to 1 and keep the rest:
ORwith 1s in those bits. For example,OR #128(OR B10000000) sets the most significant bit. - Clear bits to 0 and keep the rest:
ANDwith 0s in those bits and 1s elsewhere. For example,AND B11110000(AND &F0) clears bits 0 to 3 and keeps bits 4 to 7. - Toggle bits:
XORwith 1s in those bits.XOR B11111111flips every bit. - Clear the whole ACC:
AND #0(AND B00000000).
Practise this: past-paper questions
- June 2021 · Paper 12 · Q4(b)(i)The contents of memory address 300 represent an unsigned binary integer. Write the denary value of the unsigned binary integer in memory address 300
- June 2021 · Paper 12 · Q4(b)(ii)An assembly language program needs to test if bit number 2 in memory address 300 is a 1. Complete the assembly language instruction to perform…
- June 2021 · Paper 12 · Q4(b)(iii)An assembly language program needs to set bit numbers 4, 5, 6 and 7 to 0, but keep bits 0 to 3 with their existing values. Write the assembly…
- November 2021 · Paper 11 · Q6(b)The following table shows part of the instruction set for a processor. The processor has one general purpose register, the Accumulator (ACC), and…
Show 32 more
- November 2021 · Paper 12 · Q8(b)(i)In the following table, each row shows the current contents of the ACC in binary and the instruction that will be performed on those contents.…
- June 2022 · Paper 11 · Q6(c)(i)Complete the register to show the result after the instruction AND #2 is executed. Register before: 0 1 1 0 1 1 0 1 Register after
- June 2022 · Paper 11 · Q6(c)(ii)Complete the register to show the result after the instruction OR #8 is executed. Register before: 0 1 1 0 1 1 0 1 Register after
- June 2022 · Paper 12 · Q3(a)The ACC currently contains the following positive binary integer: 0 1 1 0 0 1 0 1 Write the bit manipulation instruction that would change the…
- June 2022 · Paper 12 · Q3(b)The ACC currently contains the following positive binary integer: 0 1 1 0 0 1 0 1 Write the bit manipulation instruction that would change the…
- June 2022 · Paper 13 · Q3(c)The table shows part of the instruction set for a processor. The processor has one general purpose register, the Accumulator (ACC). | Opcode |…
- November 2022 · Paper 11 · Q6(b)(i)The current contents of the ACC are: 1 0 0 1 0 0 1 1 Show the result after the execution of the following instruction. XOR B00011111
- November 2022 · Paper 11 · Q6(b)(ii)The current contents of the ACC are: 1 0 0 1 0 0 1 1 Show the result after the execution of the following instruction. AND B11110000
- November 2022 · Paper 11 · Q6(b)(iii)The current contents of the ACC are: 1 0 0 1 0 0 1 1 Show the result after the execution of the following instruction. OR B11001100
- November 2022 · Paper 12 · Q7(b)(i)The current contents of the ACC are: 0 1 0 1 0 0 1 1 Show the contents of the ACC after the execution of the following instruction. XOR B00011111
- November 2022 · Paper 12 · Q7(b)(ii)The current contents of the ACC are: 0 1 0 1 0 0 1 1 Show the contents of the ACC after the execution of the following instruction. AND 50
- November 2022 · Paper 12 · Q7(b)(iv)The current contents of the ACC are: 0 1 0 1 0 0 1 1 Show the contents of the ACC after the execution of the following instruction. OR 51
- November 2022 · Paper 13 · Q6(b)(i)The current contents of the ACC are: 0 0 1 1 0 1 1 0 Show the contents of the ACC after the execution of the following instruction. AND B01001100
- November 2022 · Paper 13 · Q6(b)(ii)The current contents of the ACC are: 1 0 0 1 0 1 0 1 Show the contents of the ACC after the execution of the following instruction. OR B01001111
- November 2023 · Paper 11 · Q8(c)(i)The current contents of the ACC are: 0 1 0 0 1 1 1 1 Show the contents of the ACC after the execution of the following instruction. AND B10100101
- November 2023 · Paper 11 · Q8(c)(iii)The current contents of the ACC are: 1 1 1 1 0 1 1 1 Show the contents of the ACC after the execution of the following instruction. XOR B00100101
- June 2024 · Paper 11 · Q4(b)The processor includes these bit manipulation instructions: Opcode | Operand | Explanation AND | #n/Bn/&n | Bitwise AND operation of the contents…
- June 2024 · Paper 12 · Q5(b)The processor includes these bit manipulation instructions: Opcode | Operand | Explanation AND | #n/Bn/&n | Bitwise AND operation of the contents…
- June 2024 · Paper 13 · Q3(b)The processor includes these bit manipulation instructions: | Opcode | Operand | Explanation | | AND | #n/Bn/&n | Bitwise AND operation of the…
- November 2024 · Paper 12 · Q8(b)(i)The table has three sets of instructions. The binary number 10011010 is reloaded into the ACC before each set of instructions is run. Complete the…
- November 2024 · Paper 12 · Q8(b)(ii)Explain how bit manipulation can be used to test whether the binary number stored in the ACC represents an odd denary number. Write the bit…
- November 2024 · Paper 13 · Q7(b)The instruction set also includes these bit manipulation instructions: Opcode | Operand | Explanation AND | #n/Bn/&n | Bitwise AND operation of…
- June 2025 · Paper 11 · Q8(b)(i)Write the bit manipulation instruction that can be used to set the least significant bit to 1 in an 8‑bit register. All other bits must remain…
- June 2025 · Paper 11 · Q8(b)(ii)The ACC currently contains the following binary value. 0 1 0 1 0 1 0 1 Write the result after the instruction XOR &FE is run
- June 2025 · Paper 12 · Q7(b)(i)The ACC currently contains the following binary value. 1 1 1 1 0 0 0 0 Write the result after the instruction OR B00001111 is run
- June 2025 · Paper 12 · Q7(b)(ii)The ACC currently contains the following binary value. 0 0 0 1 1 1 0 1 Write the result after the instruction XOR #30 is run
- June 2025 · Paper 13 · Q5(b)The table shows part of the instruction set for a processor. The processor has one register: the Accumulator (ACC). Opcode | Operand | Explanation…
- November 2025 · Paper 11 · Q4(b)The ACC currently contains the following positive binary integer: 1 1 1 0 0 0 1 1 Write the contents of the ACC after the instruction XOR &12 is…
- November 2025 · Paper 11 · Q4(c)The ACC currently contains the following positive binary integer: 1 1 1 0 0 0 1 1 Write the contents of the ACC after the instruction AND #63 is…
- November 2025 · Paper 11 · Q4(d)The ACC currently contains the following positive binary integer: 1 1 1 0 0 0 1 1 The current contents of memory are: Address | Data 98 | 00100100…
- November 2025 · Paper 13 · Q6(a)Complete the table by writing the contents of the ACC after the execution of each instruction. Current contents of the ACC | Instruction |…
- June 2026 · Paper 11 · Q3(b)(i)Write the bit manipulation instruction that will set the ACC to &0
4.3 · 11 of 11Book page Contents
Monitoring and controlling a device
Builds on
In a monitoring or control system, each bit of a memory location or register stands for one device: a sensor that is reporting (1 = triggered, 0 = not), or an actuator such as a light, motor or valve that is switched on (1) or off (0). One byte can hold the state of eight devices.
- Monitoring: the program repeatedly reads the byte into the ACC and uses
ANDwith a mask to test the bit for one sensor. ACMPand a conditional jump then decide what to do. - Control: the program sets a bit with
ORand a mask to switch a device on, clears it withANDand an inverted mask to switch it off, or toggles it withXOR. It then stores the byte back so that the device changes.
Using the lights register from earlier, where lights 3 and 4 (mask 00110000) are the outside lights:
// are both outside lights off?
LDD Lights
AND B00110000
CMP #0
JPE BothOff
// switch both outside lights on
LDD Lights
OR B00110000
STO Lights
// switch both outside lights off
LDD Lights
AND B11001111
STO LightsHow bit manipulation monitors or controls a device
- Each bit in a register or memory location represents the state of one device (1 on, 0 off).
- A mask picks out the bit or bits for the device.
- AND with the mask tests (checks) whether a bit is set; AND with the inverted mask clears it (off).
- OR with the mask sets the bit (on); XOR with the mask toggles it.
- The value is read and tested repeatedly, and the new value is stored back to change the device.
Practise this: past-paper questions
4.3
Concepts in 4.3
Every idea explained in 4.3, and where else in the notes it is used.
| Concept | In one line | Also used in |
|---|---|---|
| arithmetic shift | A shift for signed numbers that preserves the sign bit; a right shift copies the sign bit in. | only here |
| bit manipulation | Changing or testing individual bits using bitwise operations and shifts. | 4.3 Monitoring and controlling a device |
| bit manipulation instructions | AND, OR, XOR, LSL and LSR: the instructions that work on the bits of the ACC. | 4.2 The instruction set and instruction groups |
| bitwise AND | Gives 1 only where both bits are 1; with a mask it checks or clears bits. | 4.3 Bit manipulation instructions |
| bitwise NOT | Flips every bit: 0 becomes 1 and 1 becomes 0. | 4.3 AND: checking and clearing bits |
| bitwise OR | Gives 1 where either bit is 1; with a mask it sets bits to 1. | 4.3 Bit manipulation instructions |
| bitwise XOR | Gives 1 where exactly one bit is 1; with a mask it toggles bits. | 4.3 Bit manipulation instructions |
| cyclic shift | A shift where bits leaving one end come back in at the other end. | only here |
| logical bitwise operation | An operation (AND, OR, XOR, NOT) applied to each bit, or pair of matching bits, of a binary string. | 4.1 The processor and the Arithmetic and Logic Unit (ALU), 4.3 Masks |
| logical left shift | Moves bits left and puts 0s in on the right; each place multiplies by 2. | 4.3 Arithmetic shifts for signed numbers |
| logical right shift | Moves bits right and puts 0s in on the left; each place divides by 2. | 4.3 Arithmetic shifts for signed numbers |
| logical shift | Moves every bit left or right, discarding bits at one end and putting 0s in at the other. | 4.3 Arithmetic shifts for signed numbers |
| mask | A binary string used with a bitwise operator to pick out or change chosen bits. | 4.3 Logical bitwise operations, 4.3 AND: checking and clearing bits, 4.3 OR: setting bits, 4.3 XOR: toggling bits and clearing a register, 4.3 NOT: flipping every bit, 4.3 Bit manipulation instructions, 4.3 Monitoring and controlling a device |
| zero flag | A status register bit that shows whether a result was zero. | only here |
Zain Merchant's notes, page by page, as printed in his book. The Notes tab has the same notes as text, with links.
Past-paper questions and mark schemes © Cambridge University Press & Assessment. Compiled for revision by Zain Merchant.
- June 2021 · Paper 11 · Q3Notes:Logical shifts
- June 2021 · Paper 12 · Q4Notes:MasksAND: checking and clearing bitsBit manipulation instructions
- November 2021 · Paper 11 · Q6Notes:AND: checking and clearing bitsOR: setting bitsXOR: toggling bits and clearing a registerLogical shiftsBit manipulation instructions
- November 2021 · Paper 12 · Q8Notes:AND: checking and clearing bitsOR: setting bitsXOR: toggling bits and clearing a registerLogical shiftsBit manipulation instructions
- June 2022 · Paper 11 · Q6Notes:AND: checking and clearing bitsOR: setting bitsLogical shiftsBit manipulation instructions
- June 2022 · Paper 12 · Q3Notes:OR: setting bitsXOR: toggling bits and clearing a registerNOT: flipping every bitLogical shiftsBit manipulation instructions
- June 2022 · Paper 13 · Q3Notes:AND: checking and clearing bitsOR: setting bitsXOR: toggling bits and clearing a registerBit manipulation instructions
- November 2022 · Paper 11 · Q6Notes:AND: checking and clearing bitsOR: setting bitsXOR: toggling bits and clearing a registerLogical shiftsBit manipulation instructions
- November 2022 · Paper 12 · Q7Notes:AND: checking and clearing bitsOR: setting bitsXOR: toggling bits and clearing a registerLogical shiftsBit manipulation instructions
- November 2022 · Paper 13 · Q6Notes:AND: checking and clearing bitsOR: setting bitsLogical shiftsBit manipulation instructions
- June 2023 · Paper 11 · Q3Notes:Logical shifts
- June 2023 · Paper 12 · Q4Notes:Logical shifts
- November 2023 · Paper 11 · Q8Notes:AND: checking and clearing bitsXOR: toggling bits and clearing a registerLogical shiftsBit manipulation instructions
- June 2024 · Paper 11 · Q4Notes:AND: checking and clearing bitsOR: setting bitsXOR: toggling bits and clearing a registerBit manipulation instructions
- June 2024 · Paper 12 · Q5Notes:AND: checking and clearing bitsOR: setting bitsXOR: toggling bits and clearing a registerBit manipulation instructions
- June 2024 · Paper 13 · Q3Notes:AND: checking and clearing bitsXOR: toggling bits and clearing a registerLogical shiftsBit manipulation instructions
- November 2024 · Paper 12 · Q8Notes:MasksAND: checking and clearing bitsOR: setting bitsLogical shiftsBit manipulation instructions
- November 2024 · Paper 13 · Q7Notes:MasksAND: checking and clearing bitsBit manipulation instructions
- November 2024 · Paper 13 · Q8Notes:Arithmetic shifts for signed numbers
- June 2025 · Paper 11 · Q8Notes:MasksOR: setting bitsXOR: toggling bits and clearing a registerLogical shiftsBit manipulation instructions
- June 2025 · Paper 12 · Q7Notes:OR: setting bitsXOR: toggling bits and clearing a registerBit manipulation instructions
- June 2025 · Paper 13 · Q5Notes:MasksAND: checking and clearing bitsOR: setting bitsBit manipulation instructions
- June 2025 · Paper 13 · Q7Notes:Logical shiftsArithmetic shifts for signed numbers
- November 2025 · Paper 11 · Q4Notes:AND: checking and clearing bitsOR: setting bitsXOR: toggling bits and clearing a registerLogical shiftsBit manipulation instructions
- November 2025 · Paper 13 · Q2Notes:Arithmetic shifts for signed numbers
- November 2025 · Paper 13 · Q6Notes:AND: checking and clearing bitsXOR: toggling bits and clearing a registerBit manipulation instructions
- June 2026 · Paper 11 · Q3Notes:MasksAND: checking and clearing bitsLogical shiftsBit manipulation instructions
- June 2026 · Paper 12 · Q5Notes:Logical shiftsCyclic shifts
- June 2026 · Paper 13 · Q7Notes:Logical bitwise operationsLogical shiftsArithmetic shifts for signed numbersMonitoring and controlling a device
Past-paper questions and mark schemes © Cambridge University Press & Assessment. Compiled for revision by Zain Merchant.
Try the questions first.
Write your answers, then check them against the mark scheme. Your choice is remembered on this device.