Skip to content
Cambridge AS & A Level9618

Bit manipulation

Computer Science 9618 Chapter Notes

Last updated November 2026

ShareWhatsAppPost

Bit manipulation notes as a printable PDF.

Read online free. Download a watermarked copy with a free account.

Read the notes

The full Bit manipulation notes as text: skim, search, and jump between subtopics.

~13 min read

1. Logical shifts

A logical shift moves all bits in the register a number of places. The bits that move off the end of the register are lost. The syllabus instructions are `LSL #n` for a left shift and `LSR #n` for a right shift, where n is the number of places.

In a left shift the bits move towards the left and zeros are introduced on the right-hand end. In a right shift the bits move towards the right and zeros are introduced on the left-hand end. A logical shift puts zeros at the end that is emptied, whatever the bit was before.

Write the direction beside your working before you move any bits.

LSL #2: 0100 1111 → 0011 1100

LSR #3: 0001 0111 → 0000 0010

Examiner insight

“A small number of candidates confused left and right and so shifted the bits the wrong way.” “A small number of candidates shifted left instead of right.”

Worked example 11 mark

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`

  1. 1

    a right shift of 3 places moves every bit 3 places to the right; the last three bits, 111, are lost

  2. 2

    three zeros are introduced on the left-hand end: 0000 0010 [1]

2. Arithmetic shifts

An arithmetic shift works on a two's complement integer, so the sign matters. In an arithmetic shift right the bits move to the right, but the sign bit is copied into the Most Significant Bit (MSB). The number keeps its sign.

A logical shift right fills the vacated MSBs with 0. An arithmetic shift right fills them with copies of the sign bit. Both lose the Least Significant Bit(s) from the right of the number.

So an arithmetic right shift of 3 places on a number that starts with 1 brings in three 1s.

arithmetic right shift 3: 1001 1110 → 1111 0011

Worked example 12 marks

A computer can perform logical and arithmetic shifts.

(i) Show the result of a logical left shift of 2 places on the two's complement binary integer 11001010

(ii) Show the result of an arithmetic right shift of 3 places on the two's complement binary integer 10011110

  1. 1

    (i) move every bit 2 places left; the two leftmost bits are lost and zeros enter on the right: 0010 1000 [1]

  2. 2

    (ii) the sign bit is 1, so three copies of it enter on the left as the bits move 3 places right: 1111 0011 [1]

3. Cyclic shifts

In a cyclic shift the bits do not fall off the end. Each bit that leaves one end of the register comes back in at the other end, so every bit is kept and no zeros are introduced.

A cyclic left shift of 4 places on 10010101 moves the first four bits, 1001, to the right-hand end. The result is 0101 1001.

Some papers list `CSL #n` and `CSR #n` for the cyclic left and right shifts.

cyclic left 4: 1001 0101 → 0101 1001

Worked example 12 marks

Complete the table by writing the answer to each statement.

The denary value represented by the 8-bit unsigned binary number 01100010 after a logical right shift of 2 places has been applied.

The binary value after a cyclic left shift of 4 places has been applied to the binary number 10010101

  1. 1

    01100010 shifted right 2 places, with zeros on the left: 00011000

  2. 2

    00011000 = 16 + 8 = 24 [1]

  3. 3

    cyclic left shift of 4: the four leftmost bits 1001 move round to the right-hand end: 0101 1001 [1]

4. AND, OR and XOR on the accumulator

A bitwise operation compares the bits of the ACC with the bits of the operand in pairs, one position at a time. The three instructions are `AND`, `OR` and `XOR`. The operand is a number (`#n` denary, `Bn` binary, `&n` hexadecimal) or an `<address>`.

AND gives 1 only when both bits are 1. OR gives 1 when either bit is 1. XOR gives 1 when the two bits are different.

`AND <address>` uses the contents of memory address given, not the number. `AND 50` is not `AND #50`.

0101 0011 AND 0100 1101 = 0100 0001

0101 0011 OR 1000 1111 = 1101 1111

Examiner insight

“Some candidates need to improve their understanding of the difference between the AND #n instruction and the AND <address> instruction.” “Some candidates need to improve their understanding of the difference between the AND and OR logic operations.”

Worked example 14 marks

Memory addresses 50 and 51 hold the data in the table. 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 each of the following instructions (each starts from the same ACC).

(i) `XOR B00011111`

(ii) `AND 50`

(iii) `LSL #3`

(iv) `OR 51`

Memory addressData value
5001001101
5110001111
  1. 1

    (i) 0101 0011 XOR 0001 1111 = 0100 1100 [1]

  2. 2

    (ii) address 50 holds 0100 1101, so 0101 0011 AND 0100 1101 = 0100 0001 [1]

  3. 3

    (iii) 0101 0011 moved 3 places left, zeros on the right: 1001 1000 [1]

  4. 4

    (iv) address 51 holds 1000 1111, so 0101 0011 OR 1000 1111 = 1101 1111 [1]

5. Testing a bit with a mask

A mask is an operand chosen to leave only the bits you want to look at. `AND` with the mask keeps the bits where the mask has 1 and clears the bits where it has 0.

Bits are numbered from the right: bit 0 is the Least Significant Bit (LSB). To test one bit, AND with a mask that has a 1 in that position only. The result is zero if the bit was 0, and non-zero if it was 1.

An odd binary number has a 1 in the LSB. So the test for an odd number masks the LSB and compares the result with denary 1.

ACC AND B00000001: 1001 1010 → 0000 0000 (even)

Worked example 13 marks

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 manipulation instruction that will be used.

  1. 1

    an odd binary number will have a 1 in the Least Significant Bit (LSB) [1]

  2. 2

    a bit manipulation operation is required to access / mask only the LSB and clear all the others [1]

  3. 3

    `AND B00000001` (or `AND #1`, `AND &01`) [1]

6. Setting, clearing and toggling bits

Each of the three instructions changes bits in a different way. `OR` with a 1 sets that bit to 1 and leaves every bit under a 0 unchanged. `AND` with a 0 clears that bit and leaves every bit under a 1 unchanged. `XOR` with a 1 toggles that bit.

To clear bits 4 to 7 and keep bits 0 to 3, the mask is 0000 1111, which is `AND #15`. The mask for a clear is 1 everywhere except the bits to clear.

`AND B00000000` clears the whole byte.

OR #1: 0110 1010 → 0110 1011

AND #15: 1110 0011 → 0000 0011

Examiner insight

“This question was more challenging. A common incorrect response was AND #6.”

Worked example 12 marks

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 language instruction to perform this action.

  1. 1

    clearing bits needs `AND`: a 0 in the mask clears a bit and a 1 keeps it [1]

  2. 2

    the mask is 0000 1111, which is denary 15: `AND #15` [1]

7. Monitoring and controlling a device

Bit manipulation allows a change or an evaluation of the state of a bit, which is masking.

Monitoring: bit manipulation establishes the state of a device, which means reading inputs. It does this by testing the state of an individual bit.

Controlling: bit manipulation changes the state of a device, which means controlling outputs. It does this by setting, clearing or toggling a single bit. A byte can also be cleared, with `AND B00000000`.

Worked example 13 marks

Explain how bit manipulation can be used to monitor or control a device.

  1. 1

    when monitoring, bit manipulation can be used to establish the state of a device (read inputs) [1]

  2. 2

    ... by testing / observing the state of an individual bit [1]

  3. 3

    when controlling, bit manipulation can be used to change the state of a device (control output) ... by setting / clearing / toggling a single bit [1]

Recap

  • Logical shift: all bits move, zeros are introduced at the emptied end. LSL zeros on the right, LSR zeros on the left.
  • Arithmetic shift right copies the sign bit into the MSB. Logical shift right puts zeros there. Both lose bits from the right.
  • Cyclic shift: the bits that leave one end re-enter at the other, so nothing is lost.
  • AND 1 only if both bits are 1. OR 1 if either is 1. XOR 1 if the bits differ.
  • `AND 50` uses the contents of address 50. `AND #50` uses the number 50.
  • Mask to test a bit: AND with a 1 in that position only. Bit 0 is the LSB. An odd number has a 1 in the LSB.
  • OR with 1 sets a bit. AND with 0 clears a bit. XOR with 1 toggles a bit.
  • Monitor: test the state of a bit (read inputs). Control: set, clear or toggle a bit (change outputs).

End-of-chapter exercise

Test yourself on the whole chapter. Work through these before moving on.

  1. State what is introduced at the left-hand end of the register in a logical right shift.1 mark
    Answer
    1. Zeros.

    Trap: Saying the sign bit is copied in describes an arithmetic shift, not a logical one.

  2. The ACC contains 01101011. Write its contents after `LSL #3`.1 mark
    Answer
    1. Move every bit 3 places left. The bits 011 are lost and zeros enter on the right: 01011000.

    Trap: Shifting right instead of left, which gives 00001101.

  3. Identify one similarity and one difference between a logical right shift and an arithmetic right shift.2 marks
    Answer
    1. Similarity: both lose the Least Significant Bit(s) from the right of the number.
    2. Difference: logical fills the MSBs with 0, arithmetic with copies of the sign bit.

    Trap: Saying only that both move the bits to the right gives no difference.

  4. The two's complement integer 10110100 is in a register. Show the result of an arithmetic right shift of 2 places.2 marks
    Answer
    1. The sign bit is 1, so two copies of 1 enter on the left: 11101101.

    Trap: Filling with zeros gives the logical shift result, 00101101.

  5. The ACC contains 10110001. Show the result of a cyclic left shift of 3 places.2 marks
    Answer
    1. The three leftmost bits, 101, re-enter on the right: 10001101.

    Trap: Using zeros on the right is a logical shift. It loses the bits and gives 10001000.

  6. Bit 3 of the ACC must be tested. Bit 0 is the least significant bit. Write the instruction that masks all the other bits, using a binary operand. State the ACC after the instruction when it starts as 01011010, and say what this shows.3 marks
    Answer
    1. `AND B00001000` (or `AND #8`): 01011010 AND 00001000 = 00001000. This is not zero, so bit 3 was 1.

    Trap: Using `AND #3` (0000 0011) tests bits 0 and 1, not bit 3. Count bits from the right.

  7. The ACC contains 10101100. Memory address 40 contains 00001111. Write the ACC after each instruction, each starting from the original ACC: (a) `AND 40` (b) `OR 40` (c) `XOR 40` (d) `AND #40`4 marks
    Answer
    1. Address 40 holds 00001111. (a) AND gives 00001100. (b) OR gives 10101111. (c) XOR gives 10100011.
    2. (d) 40 is 00101000, so 10101100 AND 00101000 = 00101000.

    Trap: Treating `#40` as the contents of address 40, or `40` as the number 40.

  8. Write one instruction, with a binary operand, for each change to the ACC. All other bits must stay the same: (a) set bit 6 to 1; (b) clear bits 0 and 1; (c) toggle bit 7.3 marks
    Answer
    1. (a) `OR B01000000` (b) `AND B11111100` (c) `XOR B10000000`

    Trap: Using XOR to set a bit toggles it, so a bit that is already 1 becomes 0.

  9. A lift controller keeps its status byte in the ACC. Bit 0 is the least significant bit. The ACC contains 01101001. Memory address 12 contains 11110000 and memory address 15 contains 00000100. The instructions `AND 12`, `OR 15` and `XOR #12` run in that order, each on the result of the one before. (a) Write the ACC after each instruction. (b) The programmer meant `OR 15` to set bit 3. Explain why it does not, and write one instruction with a binary operand that sets bit 3 and changes no other bit.5 marks
    Answer
    1. (a)
    2. `AND 12`: 01101001 AND 11110000 = 01100000.
    3. `OR 15`: 01100000 OR 00000100 = 01100100.
    4. `XOR #12`: 01100100 XOR 00001100 = 01101000.
    5. (b)
    6. Address 15 holds 00000100, so `OR 15` sets bit 2. Use `OR B00001000`.

    Trap: Treating `AND 12` as the number 12 gives 00001000 and every later line is wrong.

  10. A weather station packs two readings into one byte. Bits 7 to 4 hold the wind level and bits 3 to 0 hold the rain level, each as an unsigned binary integer. The ACC contains 10110101. (a) Write one instruction that leaves the wind level in bits 3 to 0 with zeros above it, and state the wind level in denary. (b) Starting again from 10110101, write one instruction that leaves only the rain level in the ACC, and state the rain level in denary. (c) Starting again from 10110101, write two instructions that change the rain level to 9 and keep the wind level, and give the ACC after them. (d) The ACC now contains 10111001. The two halves of the byte must be swapped. Name the type of shift and the number of places, and give the result.8 marks
    Answer
    1. (a)
    2. `LSR #4` gives 00001011, which is 8 + 2 + 1 = 11.
    3. (b)
    4. `AND #15` (00001111): 10110101 AND 00001111 = 00000101, which is 4 + 1 = 5.
    5. (c)
    6. `AND B11110000` clears the rain level: 10110000.
    7. `OR B00001001` puts 9 into bits 3 to 0: 10111001.
    8. (d)
    9. A cyclic shift of 4 places (left or right): 10011011.

    Trap: `AND #6` (00000110), the mask examiners report, gives a rain level of 4.

Say this, not that

Wording that loses the mark, against the wording the mark scheme credits.

Loses the markGets the mark
‘AND 50’ is a bitwise AND with the denary value 50 (0011 0010)bitwise AND with the contents of memory address 50 (0100 1101)
‘OR 51’ is a bitwise OR with the denary value 51 (0011 0011)bitwise OR with the contents of memory address 51 (1000 1111)
shifting the bits the wrong way`LSL` moves bits to the left with zeros on the right; `LSR` moves bits to the right with zeros on the left
`AND #6` to keep bits 0 to 3`AND #15` (0000 1111) keeps bits 0 to 3 and clears bits 4 to 7

Frequently asked questions

What is a logical shift, and which bits are filled in?
A logical shift moves all the bits in a register a number of places. The bits that move off the end are lost, and zeros are introduced at the end that is emptied. LSL shifts left with zeros on the right, and LSR shifts right with zeros on the left. For example, LSR #3 turns 0001 0111 into 0000 0010.
What is the difference between a logical right shift and an arithmetic right shift?
Both move the bits to the right and lose the least significant bits. A logical right shift fills the vacated most significant bits with zeros. An arithmetic right shift works on two's complement integers and copies the sign bit into the most significant bit, so 1001 1110 shifted right 3 places gives 1111 0011.
How does a cyclic shift differ from a logical shift?
In a cyclic shift no bits are lost: each bit that leaves one end of the register comes back in at the other end, so no zeros are introduced. A cyclic left shift of 4 places on 1001 0101 gives 0101 1001. A logical shift would lose bits off the end and introduce zeros instead.
What is the difference between AND 50 and AND #50?
AND 50 uses the contents of memory address 50, which holds 0100 1101 in the example. AND #50 uses the denary number 50, which is 0011 0010 in binary. Treating AND 50 as the denary value 50 is a common mistake, so check whether the operand is an address or a number.
How can bit manipulation test whether a binary number is odd?
An odd binary number has a 1 in the least significant bit (LSB), which is bit 0. Use AND with a mask that has a 1 only in that position, such as AND B00000001, so that only the LSB is kept. Then compare the result with denary 1.
How do I set, clear and toggle bits in the ACC?
OR with a 1 sets that bit to 1 and leaves the other bits unchanged. AND with a 0 clears that bit, and XOR with a 1 toggles it. To clear bits 4 to 7 and keep bits 0 to 3, use AND #15, because the mask 0000 1111 has a 0 in each bit to clear.

Free notes are just the start.

Unlock every Workbook and Chapter at a Glance, and generate your own worksheets and predicted papers.

Explore plans

Related chapters