/en-tutor-prep-alevel-computer-science-bit-manipulation-shifts-masking/
/en-tutor-prep-alevel-computer-science-bit-manipulation-shifts-masking/
General Inquiry: info@TutorZone.com.hk
Office Hours: Monday to Saturday - 9:00 AM to 6:00 PM
WhatsApp Monday to Sunday - 24 hours
Hotline: +852 6828 1809
Instant Support: +852 9061 3106

A-Level Computer Science — Bit Manipulation: three kinds of shift, overflow and bit masking (lesson-prep guide for tutors)

A-Level Computer Science — Bit Manipulation: three kinds of shift, overflow and bit masking (lesson-prep guide for tutors)

  • 2026-11-27

Students remember that a left shift multiplies by two, then guess at a negative right shift. This chapter compares the three shifts on one byte: 10 shifted left twice is 40, minus 8 is minus 2 arithmetically but 62 logically, and 178 rotated left is 202. It then uses 181 to test, set, clear and toggle single bits.

Objectives — Students can: (1) state that an n-place left shift multiplies by two to the n and a right shift divides, computing 10 left twice as 40 and 40 right twice as 10; (2) explain overflow, that 178 shifted left once gives 100 although the true value is 356, because a bit left the eight-bit frame; (3) tell logical from arithmetic right shift, minus 8 giving 62 logically by filling with zeros and minus 2 arithmetically by filling with the sign bit, and state that arithmetic right shift rounds down rather than to nearest; (4) explain that a cyclic shift puts the bit leaving one end back at the other, so 178 rotates left to 202 and right to 172, unlike the plain left shift giving 200; (5) use a mask for four operations on 181: AND to test, giving 16 for bit 4 and 0 for bit 1, OR to set, giving 189, AND with the inverse to clear, giving 177, and XOR to toggle, giving 149; (6) explain both uses on a device, reading the sensor byte 202 as 10 in the low nibble and 12 in the high nibble, and building a control word from 0 to 1 and 5 and 4.

01 Prerequisites (3-minute check)

If any of these fails, fix it before the lesson starts.

Skill Quick check Pass mark
Binary place values What is the value of the leftmost bit in eight bits 128
Two complement negatives What is 11111000 in 8-bit two complement Minus 8
AND truth table What is 1 AND 0 0
XOR truth table What is 1 XOR 1 0, because XOR gives 0 for equal bits and 1 for different bits
Powers of two What is two to the power five 32

02 Lesson flow (60 minutes)

Time Stage Tutor does Student does
0-10 min Opening: shifting is multiplying and dividing 10 left twice and 40 right twice, writing the bits beside the arithmetic Say that an n-place left shift multiplies by two to the n
10-22 min Overflow 178 shifted left once gives 100 against a true value of 356, and 200 gives 144 Decide whether a given left shift overflows
22-34 min The three right shifts Minus 8 arithmetically is minus 2 and logically is 62; rotation gives 202 and 172 Say what each right shift fills with
34-46 min Four masking operations Test, set, clear and toggle on 181: 16, 189, 177 and 149 Write the instruction and the mask for each step
46-54 min Monitoring and controlling a device Read sensor 202 as 10 in the low nibble, then build the control word up to 5 Write the instruction that reads or sets one device
54-60 min Sanity and the exit check Ask the four common errors in turn: what a right shift fills, and which operation clears a bit State the three right shifts and how a bit is cleared

03 Teaching script (read it out)

Scene 1: Shifting multiplies, until bits fall off

Build the firmest rule first: in eight bits, a left shift by n multiplies by two to the n and a right shift divides by it. 10 is 00001010 and shifting left twice gives 00101000, which is 40, the same as 10 times four. 5 is 00000101 and shifting left three times gives 40, times eight. The other way, 40 is 00101000, shifted right twice gives 10 and right three times gives 5.
Then teach the half that students drop: overflow. 178 is 10110010, and one left shift gives 01100100, which is 100, though the true value is 356. The leftmost 1 was pushed out of the eight-bit frame and lost. 200 is 11001000, one left shift gives 10010000, which is 144, and 400 minus 256 is exactly 144. So the rule has two halves: a left shift multiplies only while no bit leaves; when one does, the result keeps just the low eight bits.
One sentence of hardware makes it stick: a shift is only a rewiring of lines, far cheaper than a multiplier, which is why compilers turn multiplication by a power of two into a shift. That is the practical reason bit manipulation is still worth teaching.
Student task: shift 00011001 left three times and right once, give both answers in denary, and say whether the left shift overflowed.

Say this: left multiplies and right divides, but if a bit leaves the frame the answer is only what stayed inside.

Scene 2: Three right shifts: zeros, the sign, or round the end

The three shifts differ only in what fills the gap. A logical right shift, LSR, fills with zeros, so a negative number turns positive: minus 8 is 11111000 and shifting right twice logically gives 00111110, which is 62. That number is meaningless as arithmetic, because the sign bit was shifted out like any other bit. An arithmetic right shift, ASR, fills with the sign bit, so a negative stays negative: 11111000 shifted right twice gives 11111110, which is minus 2, exactly minus 8 divided by 4. An arithmetic left shift behaves like a logical left shift and fills with zeros on the right: minus 8 shifted left once gives 11110000, which is minus 16.
A cyclic shift, or rotate, fills with nothing because nothing is lost: the bit leaving the right end returns at the left, and the bit leaving the left returns at the right. 178 is 10110010; rotating left twice first gives 11001000 and then puts the two bits that left, 10, into the lowest two places, giving 11001010, which is 202. Rotating the same byte right twice gives 10101100, which is 172. Let students find the difference themselves: a plain left shift of 178 gives only 200, and the 202 of the rotation differs by exactly the two bits that came back.
Why rotate at all? Because some data is not a number but a set of flags or a bit string, and encryption, cyclic redundancy checks and horizontal scrolling all need every bit kept. Said that way, nobody mistakes a rotation for another kind of multiplication.
Student task: rotate 10010110 left three times, then say what a plain left shift by three gives and why the two answers differ.

Say this: logical fills zeros, arithmetic fills the sign, cyclic brings the bit back; all three are called a right shift and all three give different answers.

Scene 3: Masks: test, set, clear, toggle

A mask works by operating on a byte with a purpose-built byte in which the bits you want to touch are 1 and the rest are 0. Use 181, which is 10110101, for the four operations that exams always ask separately.
Test uses AND: to learn whether bit 4, worth 16, is 0 or 1, AND with 00010000. The result is 16, not zero, so the bit is 1; testing bit 1 with 00000010 gives 0, so that bit is 0. Be precise that the result of AND is not a yes or no: non-zero means 1 and zero means 0, and writing 16 as the answer loses the mark.
Set uses OR: to force bit 3 to 1, OR with 00001000, giving 10111101, which is 189, with the other seven bits unchanged. Clear uses AND with the inverse mask: to force bit 2 to 0, the mask is 00000100, its inverse is 11111011, and the AND gives 10110001, which is 177. Toggle uses XOR: to flip bit 5, XOR with 00100000, giving 10010101, which is 149.
Why must clearing use the inverse mask? Because OR only ever turns a bit into 1 and can never remove a 1; removing a 1 requires AND with a 0. Have students repeat that sentence, because it is the most examined judgement in the chapter.
Finally the device, which answers why masks exist. Monitoring: a sensor byte 202, which is 11001010, carries a temperature in the low nibble, so AND with 00001111 gives 10, and a humidity in the high nibble, so shifting right four places gives 12. Counting how many sensors have triggered means counting the 1 bits, four of them. Control: three devices each own one bit, a pump at bit 0, a fan at bit 1 and a heater at bit 2. Starting from 0, setting the pump bit gives 1, setting the heater bit as well gives 5, and clearing the pump bit gives 4. The whole control word has eight states from 0 to 7, each one a combination of devices, and that is how bit manipulation is used in embedded systems.
Student task: give the control word for fan only and for all three devices on, then say which operation and which mask turn all three into pump only.

Say this: test with AND, set with OR, clear with AND and the inverse, toggle with XOR; four actions, four different routes.

04 Question types and worked steps

Type Example Steps Answer
Evaluate a shift 10 (00001010) left twice, and 40 (00101000) right twice Multiply or divide by two to the n, writing the bits beside the arithmetic 40 and 10
Detect overflow 178 left once and 200 left once Count the 1 bits that would leave the left end; if any leave, keep only the low eight bits 100 and 144 against true values of 356 and 400, both overflowing
Tell the three right shifts apart Minus 8 (11111000) right twice Ask what fills the gap: zeros for logical, the sign for arithmetic, or the bit returns for cyclic logical 62, arithmetic minus 2
Rotate 178 (10110010) rotated left and right twice Shift normally, then put the bits that left back at the other end 202 (11001010) and 172 (10101100), where a plain left shift gives only 200
Four masking operations 181 (10110101): test bit 4, set bit 3, clear bit 2, toggle bit 5 AND to test, OR to set, AND with the inverse to clear, XOR to toggle, changing one bit each time 16 non-zero, 189, 177, 149
Sanity check (teach this): Five checks. First, watch the bits and the value together: 10 shifted left twice is 40, and an answer of 20 means only one place was shifted. Second, an overflowed answer is smaller than the true value: 178 shifted left gives 100 against 356, so an answer above the true value means the eight-bit frame was ignored. Third, a negative right shift depends on what fills the gap: logical gives 62, a positive, and arithmetic gives minus 2, and both can be computed, so check the filling rule rather than which number looks nicer. Fourth, a bit is cleared only with AND and an inverse mask: OR on 181 produces something like 189, larger, and can never remove a 1. Fifth, a masking result is not a boolean: testing bit 4 gives 16 or 0, which must then be interpreted as the bit being 1 or 0, and 16 on its own is not the answer.

05 Independent practice (3 questions)

Question Answer What to watch
What are 25 (00011001) shifted left three times and right once? And what is minus 25 (11100111) shifted right once arithmetically? Left three times gives 200 and right once gives 12, because a low bit was lost and the shift rounds down; minus 25 arithmetically gives -13 A negative arithmetic right shift rounds down, not to nearest: minus 25 divided by 2 is minus 12.5, and the shift gives -13
A status byte is 105 (01101001). What does testing bit 3 give, what is the byte after setting bit 7, and after clearing bit 0? Testing bit 3 with 00001000 gives 8, non-zero, so the bit is 1; setting bit 7 with 10000000 gives 233; clearing bit 0 with 11111110 gives 104 A test only answers whether the bit is 1 or 0; a set and a clear must both name the operation used
What is 150 (10010110) rotated left three times, and what would a plain left shift by three give, and why are they different? The rotation gives 180 (10110100); the plain shift gives 176, because the top three bits 100 left the frame and never came back A rotation returns the bits that leave to the other end, so all eight bits survive

06 Common mistakes and what to say

Mistake Student writes Say this
Assuming every right shift fills zeros Saying minus 8 shifted right twice gives 62 Only a logical right shift fills zeros, giving 62, with the sign shifted out like any other bit; an arithmetic right shift fills the sign bit and gives minus 2, so keep the sign when the question says arithmetic.
Treating a rotation as a plain shift Saying 178 rotated left twice gives 200 200 is the plain shift, where the bits 10 fell off; the rotation returns them to the lowest two places, giving 202.
Clearing a bit with OR Saying bit 2 is cleared by OR with 00000100 OR only turns bits into 1 and can never remove a 1; clearing needs AND with 11111011, giving 177.
Assuming a left shift always multiplies Saying 178 shifted left once gives 356 356 is the mathematical value but does not fit in eight bits; the leftmost 1 left the frame, so the answer is 100, which is overflow.

07 Homework (paper, section and question numbers)

08 Exit check (last 5 minutes)

Question Answer Decision
One difference Minus 8 logically is 62 and arithmetically is minus 2 Saying they are the same means scene 2 needs redoing
One operation Clearing a bit needs AND with the inverse, giving 177 Saying OR means scene 3 needs redoing
One figure 178 shifted left once gives 100 against a true value of 356 Saying 356 means overflow was ignored

09 Tutor FAQ

Why can a bit not be cleared with OR?

Because OR only turns bits into 1: any bit OR 1 is 1, and OR 0 leaves it unchanged, so OR can never turn a 1 into a 0. Clearing needs AND with a mask that has a 0 in that position, which is the original mask inverted, so clearing bit 2 means AND with 11111011.

How do I know whether a question wants arithmetic or logical?

Look for wording about keeping the sign, or for a value that is negative and must stay negative. An arithmetic shift is for signed integers; a logical shift is for bit strings, flags and images. Both agree on positive values, so a question separates them with a negative, such as minus 8 giving minus 2 or 62.

What is a rotation actually used for?

When the data is not a number but a string of bits: bit rotation in encryption, cyclic redundancy checks, horizontal scrolling in graphics, or moving the top bit of a byte to the bottom to test it. A plain shift loses bits and a rotation never does.

Find a tutor — freeTutor Registration →

A TutorZone lesson-prep guide. Written by the TutorZone team; questions and lesson flow are original and are not taken from any school or publisher material.

Join as a tutor or coach — academic, music, sports, arts, STEM. Free to register. See how it works →