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.
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.
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.
| Shift | What happens | Example (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) |
| Arithmetic | Used 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 |
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.
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.
| Job | Operation | Mask | Why it works |
|---|---|---|---|
| Test a bit | AND | 1 in the bit to test, 0 elsewhere | Result is 0 if the bit was 0, not 0 if it was 1 |
| Set a bit (to 1) | OR | 1 in the bit to set, 0 elsewhere | x OR 1 = 1; x OR 0 = x |
| Clear a bit (to 0) | AND | 0 in the bit to clear, 1 elsewhere | x AND 0 = 0; x AND 1 = x |
| Toggle a bit | XOR | 1 in the bit to flip, 0 elsewhere | x 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.
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.
Four quick checks. Each correct answer earns XP and records this skill on your profile.
In the lab, what enters on the right in a logical left shift?
What is the effect of LSL #1 on an unsigned binary number, if no 1 is lost?
Which operation and mask set bit 0 to 1 without changing the other bits?
Which shift never loses any bits?
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.
Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.
The ACC holds 01101100. What does it hold after LSR #2?
The ACC holds B10110110. What does it hold after AND B00001111?
The ACC holds &3C. What does it hold after XOR &FF?
The 8-bit two's complement value 11110000 (−16) has an arithmetic shift right of 1 place. What is the result?
This skill is now recorded on your profile. Keep the chain going.