AS Level · Topic 4.3
A-Level Computer Science 9618 / Topic 4 / AS

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. To control them you must change one bit without disturbing the other seven. Learn the three kinds of shift and how AND, OR and XOR masks test, set, clear and flip individual bits.

The key idea

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
Section 01

Three kinds of shift.

In the lab, shift a byte left and right and watch the zeros enter and the end bits fall off. That is a logical shift; arithmetic and cyclic shifts differ only in what fills the gap.

ShiftWhat happensExample (8 bits)
Logical left (LSL #n)Bits move n places left; zeros enter on the right; bits leaving the left are lost. Multiplies an unsigned number by 2n if no 1s are lost.00010110 (22), LSL #2 gives 01011000 (88)
Logical right (LSR #n)Bits move n places right; zeros enter on the left; bits leaving the right are lost. Divides an unsigned number by 2n, losing any remainder.10110100 (180), LSR #3 gives 00010110 (22)
ArithmeticUsed for signed (two's complement) numbers: the sign bit is preserved. In a right shift the sign bit is copied into the empty places on the left. In a left shift zeros enter on the right, and the result is only valid while the sign bit is unchanged.11101000 (−24), arithmetic right 1 gives 11110100 (−12)
Cyclic (rotate)Bits that leave one end re-enter at the other end, so no bits are lost.10010011, cyclic left 1 gives 00100111
Your turn
Apply a cyclic shift of 2 places to the right to 10010011.
Examiner trap

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.

Section 02

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. Each combines the ACC with the operand bit by bit and leaves the result in the ACC.

JobOperationMaskWhy it works
Test a bitAND1 in the bit to test, 0 elsewhereResult is 0 if the bit was 0, not 0 if it was 1
Set a bit (to 1)OR1 in the bit to set, 0 elsewherex OR 1 = 1; x OR 0 = x
Clear a bit (to 0)AND0 in the bit to clear, 1 elsewherex AND 0 = 0; x AND 1 = x
Toggle a bitXOR1 in the bit to flip, 0 elsewherex XOR 1 = NOT x; x XOR 0 = x

Bits are numbered from 0 on the right to 7 on the left. In Fig. 1, 01101010 AND 00001000 gives 00001000, which is not zero, so bit 3 is 1.

Worked example Two changes at once
A greenhouse controller byte at address CONTROL holds B01000101. Bit 4 switches the heater and bit 0 switches the fan. Turn the heater on and the fan off, leaving every other bit unchanged. Give the instructions and the final value in binary and hexadecimal.
  1. LDD CONTROL loads 01000101 into the ACC.
  2. To set bit 4, OR with B00010000: 01000101 OR 00010000 = 01010101.
  3. To clear bit 0, AND with B11111110: 01010101 AND 11111110 = 01010100.
  4. STO CONTROL stores the result. 01010100 = 0101 0100 = &54 (84 in denary).
Answer: LDD CONTROL, OR B00010000, AND B11111110, STO CONTROL; 01010100 = &54
Examiner trap

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.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

In the lab, what enters on the right in a logical left shift?

Quick check+10 XP

What is the effect of LSL #1 on an unsigned binary number, if no 1 is lost?

Quick check+10 XP

Which operation and mask set bit 0 to 1 without changing the other bits?

Quick check+10 XP

Which shift never loses any bits?

Section 03

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. Devices are monitored by testing bits that sensors set, and controlled by setting the bits that switch actuators.

        LDD SENSOR
        AND B01000000
        CMP #0
        JPN ALARM
        ...
ALARM:  LDD CONTROL
        OR B10000000
        STO CONTROL
        END
SENSOR: B01001001
CONTROL: B00000000

Bit 6 of SENSOR is set by a smoke detector. AND B01000000 keeps only bit 6. If the result is not 0 the compare with #0 is False, so JPN jumps to ALARM, which sets bit 7 of CONTROL to switch on the siren.

Your turn
With SENSOR holding B01001001, does the program jump to ALARM? What does CONTROL hold afterwards?
Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

The ACC holds 01101100. What does it hold after LSR #2?

Exam style+20 XP

The ACC holds B10110110. What does it hold after AND B00001111?

Exam style+20 XP

The ACC holds &3C. What does it hold after XOR &FF?

Exam style+20 XP

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

Skill unlocked

Shifts, masks and bit manipulation, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Logical, arithmetic and cyclic shifts, and AND, OR and XOR masks to test, set, clear and toggle bits to monitor and control devices.