Teachers' Portal  /  A-Level Computer Science 9618  /  Processor fundamentals  /  Shifts, masks and bit manipulation
Lesson pack · A-Level Computer Science 9618 · 4.3 · AS Level

Shifts, masks and bit manipulation

Every bit counts. A single byte can hold the on or off state of eight different devices: a heater, a fan, a light, an alarm.

Student lesson Lab, full screen
At a glance

The shape of the lesson

Topic
Processor fundamentals
Syllabus reference
Cambridge International AS and A Level Computer Science 9618, 4.3
Level
AS Level
Duration
45 minutes, lab-led; stretches to a 60 minute block (see the contingency notes)
Position in topic
Lesson 8 of 8 in Processor fundamentals; follows Tracing an assembly language program
Central model
Fig. 1: one byte, four masks: test, set, clear and toggle
Cooperative structure
Assessment
Stage 1 check and the exam-style plenary from the student lesson, plus the worksheet as the exit task
Learning objectives

By the end of the lesson, learners can

Section by section
  • Three kinds of shift: In the lab, shift a byte left and right and watch the zeros enter and the end bits fall off.
  • Test, set, clear, toggle: The 9618 instruction set has AND, OR and XOR, each with an immediate operand (#n, Bn or &n) or an address.
  • Monitor and control: Run the "Bit masking" program in the lab: it tests one bit of a status byte, outputs Y or N, then sets another bit.
Key points to leave on the board
  • Logical: zeros fill the gap; left multiplies, right divides (unsigned).
  • Arithmetic: keeps the sign bit; cyclic: bits wrap round.
  • Test: AND with 1 in that bit, then CMP #0. Clear: AND with 0 in that bit, 1s elsewhere.
  • Set: OR with 1 in that bit. Toggle: XOR with 1 in that bit.
The core visual model

One picture every idea returns to

In a logical shift, bits move left or right and zeros fill the empty places (LSL #n, LSR #n). An arithmetic shift keeps the sign bit, so a two's complement number keeps its sign. In a cyclic shift (rotate), a bit leaving one end re-enters at the other. A mask is a bit pattern combined with the ACC: AND with a 1 in one place tests that bit (and AND with a 0 clears it), OR with a 1 sets a bit, and XOR with a 1 flips it, while all other bits are left unchanged.

masking the byte 01101010 test bit 3AND 00001000= 00001000 set bit 0OR 00000001= 01101011 clear bit 6AND 10111111= 00101010 toggle bit 7XOR 10000000= 11101010 not zero, so bit 3 is 1 1 forces the bit on 0 forces the bit off 1 flips the bit bits are numbered 7 (left) to 0 (right)
Fig. 1: one byte, four masks: test, set, clear and toggle
Lesson sequence

Forty-five minutes, phase by phase

TimePhaseWhat happens in the roomGrouping
0 to 5 minHook: predictOpen with the question in the title, Every bit counts.. A single byte can hold the on or off state of eight different devices: a heater, a fan, a light, an alarm. Put the figure on the board: it is the one picture every idea in the lesson returns to.Whole class, then pairs
5 to 13 minTeach and check: Three kinds of shiftIn the lab, shift a byte left and right and watch the zeros enter and the end bits fall off. Project tab 4 of the lab (Logical shifts) and let a student drive it while the class predicts. Your turn: Apply a cyclic shift of 2 places to the right to 10010011.Whole class, then pairs
13 to 21 minTeach and check: Test, set, clear, toggleThe 9618 instruction set has AND, OR and XOR, each with an immediate operand (#n, Bn or &n) or an address. Model the worked example Two changes at once on the board, one line at a time.Pairs
21 to 30 minTeach and check: Monitor and controlRun the "Bit masking" program in the lab: it tests one bit of a status byte, outputs Y or N, then sets another bit. Project tab 5 of the lab (Bit masking in assembly language) and let a student drive it while the class predicts. Your turn: With SENSOR holding B01001001, does the program jump to ALARM? What does CONTROL hold afterwards?Corners
30 to 36 minCheckThe four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong.Individual, then whole class
36 to 43 minPlenaryThe exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording.Individual, random call
43 to 45 minExit and homeworkSet the worksheet below (6 questions, 19 marks). Learners self-assess against the objectives before they leave.Individual
Timing and contingency

Protect the parts that carry the learning

The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.

Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.

Cut first if the build overruns: the last check in Monitor and control, never the check or the plenary.

In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.

Running the cooperative task

Corners

Put one answer or viewpoint in each corner of the room. Students move to the corner they agree with, justify the choice to a partner there, then hear one voice from each corner before the reveal.

The full structure, with its variations and the other twelve, is on the cooperative-learning page.

Examiner traps to pre-empt

What to head off, and how

Trap learners fall intoTeaching move that pre-empts it
A logical right shift of a negative two's complement number does not halve it: the zero entering on the left makes it positive.Use an arithmetic shift to keep the sign.
To clear a bit, the mask has a 0 in that bit and 1s everywhere else.A mask of 00000001 with AND clears every bit except bit 0.
Worksheet

Shifts, masks and bit manipulation: worksheet

6 questions, 19 marks. Written in the style of the papers; no past paper question is reproduced.

NameClassDate
Q1 · recall[3 marks]

Describe the difference between a logical shift, an arithmetic shift and a cyclic shift.

Q2 · calculate[4 marks]

The ACC holds the unsigned value 00110101. Give the contents of the ACC after (a) LSL #2 and (b) LSR #3, each applied to the original value. State the effect of each shift on the denary value.

Q3 · calculate[2 marks]

The 8-bit two's complement value 10011000 is shifted 2 places to the right using an arithmetic shift. Give the result in binary and in denary.

Q4 · write[3 marks]

Bit 3 of the byte at address STATUS switches on a pump. Write assembly language instructions, using the 9618 instruction set, to switch the pump on without changing any other bit.

Q5 · write[4 marks]

Bit 6 of the byte at address SENSOR is 1 when a door is open. Write assembly language instructions, using the 9618 instruction set, to jump to the label DOOROPEN if the door is open.

Q6 · analysis[3 marks]

The ACC holds 11010010. Give the contents of the ACC after XOR B00001111 and explain the effect of this mask.

Answer key and marking notes
Q1 · 3 marks
  • Logical: bits move left or right, zeros are introduced at the empty end and bits shifted out are lost.
  • Arithmetic: the sign bit is preserved (in a right shift the sign bit is copied into the empty places), so signed numbers keep their sign.
  • Cyclic: bits shifted out of one end are moved in at the other end, so no bits are lost.
Q2 · 4 marks
  • (a) 11010100.
  • (a) 53 becomes 212: multiplied by 4.
  • (b) 00000110.
  • (b) 53 becomes 6: divided by 8 with the remainder lost (the bits 101 are lost).
Q3 · 2 marks
  • 11100110.
  • −104 becomes −26 (divided by 4).
Q4 · 3 marks
  • LDD STATUS
  • OR B00001000 (or OR #8 / OR &08)
  • STO STATUS
Q5 · 4 marks
  • LDD SENSOR
  • AND B01000000 (mask to keep only bit 6)
  • CMP #0
  • JPN DOOROPEN (or CMP B01000000 then JPE DOOROPEN)
Q6 · 3 marks
  • 11011101.
  • The four least significant bits are inverted (toggled).
  • The four most significant bits are unchanged, because XOR with 0 leaves a bit as it is.
Quiz

6 multiple-choice questions on this lesson

From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.

Q11 mark

The accumulator holds 00101101. What does it hold after LSL #2?

  • A10110100
  • B00001011
  • C01011010
  • D10110111
Q21 mark

Which instruction clears bit 5 of the accumulator to 0 and leaves every other bit unchanged? (Bits are numbered 7 on the left to 0 on the right.)

  • AAND B11011111
  • BAND B00100000
  • COR B11011111
  • DXOR B11011111
Q31 mark

The accumulator holds &5A. What does it hold after XOR &0F?

  • A&55
  • B&5F
  • C&0A
  • D&A5
Q41 mark

The 8-bit two's complement value 10100100 (−92) has an arithmetic shift of 1 place to the right. What is the result?

  • A11010010 (−46)
  • B01010010 (82)
  • C01001000 (72)
  • D10010010 (−110)
Q51 mark

The byte 10110001 is given a cyclic shift of 3 places to the right. What is the result?

  • A00110110
  • B00010110
  • C10001101
  • D11110110
Q61 mark

Address 120 holds B10010110. What is output when this program runs? (ASCII: 89 is Y, 78 is N.)

100  LDD 120
101  AND B00000100
102  CMP #0
103  JPE 107
104  LDM #89
105  OUT
106  END
107  LDM #78
108  OUT
109  END

  • AY, because bit 2 is 1, so the AND result is not 0 and JPE does not jump
  • BN, because the AND result is not 0, so the compare is True and JPE jumps
  • CN, because bit 2 of 10010110 is 0, so the AND result is 0
  • DY, because the AND result is 0, so the compare is False and JPE does not jump
Quiz key
Q1: A

Each bit moves two places left, the two leading 0s are lost and two 0s enter on the right: 10110100 (45 × 4 = 180). 00001011 is a shift to the right, 01011010 is a shift of only one place, and 10110111 fills the gap with 1s, but a logical shift always brings in 0s.

Q2: A

x AND 0 = 0 and x AND 1 = x, so AND with 0 in bit 5 and 1s elsewhere clears only bit 5. AND B00100000 clears every bit except bit 5. OR B11011111 sets seven bits to 1 and leaves bit 5 alone. XOR B11011111 flips seven bits and leaves bit 5 unchanged.

Q3: A

&5A = 0101 1010 and &0F = 0000 1111. The low four bits are flipped, 1010 becomes 0101, and the high four are unchanged: 0101 0101 = &55. &5F is the result of OR, &0A the result of AND, and &A5 flips all eight bits, as XOR &FF would.

Q4: A

Each bit moves one place right and the sign bit 1 is copied into the empty place: 11010010, which is −46, half of −92. 01010010 is a logical shift, where a 0 enters and the number becomes positive. 01001000 is a shift to the left, and 10010010 keeps the sign bit but fills the gap with 0, which does not halve the value.

Q5: A

The three right-hand bits, 001, leave the right and re-enter on the left in the same order, in front of 10110: 00110110. 00010110 is a logical right shift, which loses those bits and brings in 0s. 10001101 is a cyclic shift to the left, and 11110110 is an arithmetic right shift, which copies the sign bit.

Q6: A

10010110 AND 00000100 = 00000100, because bit 2 (the third from the right) is 1. That is not 0, so CMP #0 is False, JPE does not jump, and LDM #89 then OUT outputs Y. The compare tests equality with 0, so a non-zero result makes it False, not True. Bit 2 is 1, not 0 (counting from the left gives the wrong bit), and the AND result is not 0.

Lab and demo

What to project

The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.

Slides

Slides for this lesson

A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.

Also for this topic

Everything else you can use

Original work by the TheLucidSTEM team. The worksheet items are written in the style of the papers; no past paper question is reproduced.
Previous lessonTracing an assembly language program