CSwithZain
WhatsApp Join a class

AS Level 9618 · Paper 1 · 4.3

Bit manipulation

Shifting and masking bits, and using them to monitor and control a device. Read the notes, try the past-paper questions, then check the mark scheme.

Past papers from June 2021 to June 202629 questions · 89 marks

    In this subtopic

    • Logical, arithmetic and cyclic shifts
    • AND, OR and XOR masks to test, set, clear and toggle bits
    • Using bit manipulation to monitor and control a device

    4.3 · 1 of 11Book page Contents

    Logical bitwise operations

    In high-level programming languages, programmers rarely work directly with binary strings. Bit manipulation lets a programmer work with sets of bits using logical bitwise operations. These operations are directly supported by the processor, which makes them fast and very efficient.

    The logical bitwise operators AND, OR, XOR and NOT can be used with a mask to pick out or change specific bits. Binary shifts (logical, arithmetic and cyclic) can be used to move the bits, for example within a register.

    A logical bitwise operator works on a single bit (or a set of bits) within a binary string. Bitwise operations are often used in low-level programs to change the contents of registers or memory locations.

    For example, in a monitoring and control system, a single 8-bit register or memory location could hold the inputs from eight sensors, or control eight actuators. Logical bitwise operators can be used with a mask to check, set, clear or toggle (flip) each bit, or set of bits, in the string.

    4.3 · 2 of 11Book page Contents

    Masks

    A mask is a binary string that is used with a logical bitwise operator to pick out or change a single bit (or set of bits) in another binary string.

    For example, each bit in a processor’s status register has its own meaning. Suppose the first bit shows whether the result of an arithmetic operation was zero; this is called the zero flag. That bit can be examined, set or reset using a mask of the same length with a 1 in the position of the zero flag and 0 everywhere else. Here the mask would be 10000000, because the zero flag is in the first position.

    4.3 · 3 of 11Book pages Contents

    AND: checking and clearing bits

    The logical bitwise AND operation takes two binary strings of the same length and ANDs each pair of matching bits. The result in each position is 1 if both bits are 1; otherwise it is 0.

    AND can be used to find out whether a particular bit (or group of bits) is set (1) or clear (0). It can also be used to clear selected bits, that is, to set them to 0.

    Consider a control system that uses an 8-bit register to control eight lights. If the bit for a light is 1, the light is on; if it is 0, the light is off. This register shows that lights 1, 4, 5 and 8 are on and lights 2, 3, 6 and 7 are off:

    Light number12345678
    Register10011001

    Lights 3 and 4 are security lights outside the building. To pick out the bits that control these two outside lights, the mask 00110000 is used:

    Light number12345678
    Mask00110000

    Checking selected bits. To check whether either of the outside lights is on, AND the register with the mask. If the result is not zero (00000000), at least one of those lights is on.

    Light number12345678
    Register10011001
    Mask00110000
    AND00010000

    The result is not zero, so at least one outside light is on (light 4).

    Clearing selected bits. To turn the outside lights off, AND is used again, but now the mask must have 0 in the positions to clear and 1 everywhere else. So first apply a bitwise NOT to the mask to flip its bits:

    Light number12345678
    Mask00110000
    NOT11001111

    Then AND the register with the inverted mask:

    Light number12345678
    Register10011001
    Inverted mask11001111
    AND10001001

    Only the bit in position 4 (light 4) has changed. Light 3 was already off, so bit 3 did not need to change. All the other bits keep their original values.

    Practise this: past-paper questions

    Show 16 more

    4.3 · 4 of 11Book page Contents

    OR: setting bits

    Builds on

    The logical bitwise OR operation takes two binary strings of the same length and ORs each pair of matching bits. The result in each position is 1 if either or both bits are 1; if both bits are 0, the result is 0.

    AND with a mask is used to check or clear bits. OR with a mask is used to set one or more bits to 1.

    To set (turn on) the outside lights, use the same register, the same mask (00110000) and OR:

    Light number12345678
    Register10011001
    Mask00110000
    OR10111001

    The bit in position 3 (light 3) has been set to 1; it was 0. All the other bits are unchanged. Light 4 was already on, so bit 4 did not need to change.

    Practise this: past-paper questions

    Show 11 more

    4.3 · 5 of 11Book pages Contents

    XOR: toggling bits and clearing a register

    The logical bitwise XOR operation takes two binary strings of the same length and XORs each pair of matching bits. The result in each position is 1 if exactly one of the two bits is 1; if both bits are 0, or both are 1, the result is 0.

    XOR with a mask is used to toggle bits: a bit that is 1 becomes 0, and a bit that is 0 becomes 1.

    To toggle the outside lights, use the same register, the same mask and XOR:

    Light number12345678
    Register10011001
    Mask00110000
    XOR10101001

    The bits in positions 3 and 4 have been toggled (flipped). All the other bits are unchanged.

    Clearing all the bits. XOR is also an efficient way to set a register to zero. XOR of two bits that are the same gives 0, so XOR the register with a copy of itself:

    Light number12345678
    Register10011001
    Mask (copy of the register)10011001
    XOR00000000

    This usually needs fewer clock cycles and less memory than loading the value zero and saving it to the register.

    Practise this: past-paper questions

    Show 10 more

    4.3 · 6 of 11Book page Contents

    NOT: flipping every bit

    The logical bitwise NOT operation flips every bit in a binary string: bits that are 0 become 1, and bits that are 1 become 0. For example:

    Bit12345678
    Binary string10011001
    NOT01100110

    Note There is no NOT instruction in the Cambridge instruction set. To flip every bit of the ACC, use XOR B11111111 (XOR with all 1s). To flip only some bits, XOR with a mask that has 1s in those positions.

    4.3 · 7 of 11Book pages Contents

    Logical shifts

    A logical left shift moves all the bits in a binary string a given number of places to the left. A left shift by one place means:

    • moving all the bits one place to the left
    • discarding the most significant (leftmost) bit
    • putting a 0 into the empty place on the right

    If the string represents a number, each shift one place left multiplies the number by 2. So shifting three places left multiplies the number by 23 = 8. For example, to multiply 14 by 8, shift its binary value left three times to get 112:

    Denary1286432168421
    1400001110
    2800011100
    5600111000
    11201110000

    In general, shifting a binary number left by n places multiplies it by 2n.

    A logical right shift moves all the bits a given number of places to the right. A right shift by one place means:

    • moving all the bits one place to the right
    • discarding the least significant (rightmost) bit
    • putting a 0 into the empty place on the left

    If the string represents a number, each shift one place right divides the number by 2. In general, shifting a binary number right by n places divides it by 2n. For example, shifting 01110000 (112) right three places gives 00001110 (14), and 112 ÷ 8 = 14.

    Note Bits shifted out at the end are lost. If a 1 is shifted out, the answer is no longer exact: a left shift has overflowed, or a right shift has lost the remainder (for example 7 shifted right one place gives 3).

    In the Cambridge instruction set, LSL #n shifts the bits in the ACC n places to the left and LSR #n shifts them n places to the right; zeros are introduced at the other end.

    Practise this: past-paper questions

    Show 16 more

    4.3 · 8 of 11Book pages Contents

    Arithmetic shifts for signed numbers

    Logical shifts cannot be used to multiply or divide signed numbers, because the sign bit must not simply be shifted like the other bits. If a number is signed, an arithmetic shift must be used.

    An arithmetic shift is used when the binary string represents a signed number, stored with the sign as the most significant bit (for example, in two’s complement). It makes sure that shifting does not change the sign by accident: the sign of the number is preserved.

    • In an arithmetic right shift, the bits move right and the sign bit is copied into the empty place on the left, so a negative number stays negative. Each place divides the number by 2.
    • In an arithmetic left shift, the bits move left and 0s are put in on the right, just as in a logical left shift. Each place multiplies the number by 2, as long as the sign bit does not change. If it would change, the result is too large to store: overflow.

    Worked example

    The 8-bit two’s complement integer 11001000 represents −56. Shift it one place to the right, first arithmetically, then logically.

    1. Step 1
      Arithmetic right shift: move every bit one place right and copy the sign bit (1) into the left-hand end. The answer is −28, which is −56 ÷ 2.
      11001000  →  11100100   (−28)
    2. Step 2
      Logical right shift: a 0 comes in on the left, so the sign bit changes and the answer is wrong.
      11001000  →  01100100   (+100)
    3. Step 3
      So an arithmetic shift must be used for signed numbers.

    Right logical shift compared with right arithmetic shift

    • Similarity: both move every bit to the right, and the least significant bit is lost.
    • Difference: a logical shift puts 0s in at the left; an arithmetic shift copies the sign bit into the left, so the sign is preserved.

    4.3 · 9 of 11Book page Contents

    Cyclic shifts

    In a cyclic shift, no bits are lost: the bits that are shifted out at one end are moved into the other end of the register.

    Worked example

    Show the result of a cyclic left shift and a cyclic right shift of one place on 10110001.

    1. Step 1
      Cyclic left shift: every bit moves one place left, and the leftmost bit (1) moves into the right-hand end.
      10110001  →  01100011
    2. Step 2
      Cyclic right shift: every bit moves one place right, and the rightmost bit (1) moves into the left-hand end.
      10110001  →  11011000
    3. Step 3
      A cyclic shift of 8 places on an 8-bit register gives back the original value, because every bit has gone all the way round.

    4.3 · 10 of 11Contents

    Bit manipulation instructions

    The syllabus adds these instructions to the instruction set in 4.2. Each row gives the opcode, the operand and what the instruction does:

    OpcodeOperandExplanation
    AND#n / Bn / &nBitwise AND operation of the contents of ACC with the operand
    AND<address>Bitwise AND operation of the contents of ACC with the contents of <address>
    XOR#n / Bn / &nBitwise XOR operation of the contents of ACC with the operand
    XOR<address>Bitwise XOR operation of the contents of ACC with the contents of <address>
    OR#n / Bn / &nBitwise OR operation of the contents of ACC with the operand
    OR<address>Bitwise OR operation of the contents of ACC with the contents of <address>
    LSL#nBits in ACC are shifted logically n places to the left. Zeros are introduced on the right hand end
    LSR#nBits in ACC are shifted logically n places to the right. Zeros are introduced on the left hand end
    LabelOpcodeOperandExplanation
    <label>:<opcode><operand>Labels an instruction
    <label>:<data>Gives a symbolic address <label> to the memory location with contents <data>

    The same rules apply as in 4.2: only one general purpose register (ACC), IX is the Index Register, <address> can be absolute or symbolic, and #123 is denary, B01001010 is binary and &4A is hexadecimal. Watch the difference: AND #15 uses the number 15, but AND 15 uses the contents of address 15.

    Exam questions usually number the bits of a byte from 7 (the most significant bit, on the left, worth 128) down to 0 (the least significant bit, on the right, worth 1). Bit n is worth 2n, so a mask for bit 2 is 00000100, which is #4.

    Bit number76543210
    Place value1286432168421

    Worked example: test a bit

    The value in the memory location labelled Sensors is 10110110. Test whether bit 2 is 1, and jump to Alarm if it is.

    1. Step 1
      Load the value and AND it with a mask that has a 1 only in bit 2. Every other bit becomes 0.
      LDD Sensors   // 10110110
      AND #4        // 00000100
    2. Step 2
      If bit 2 was 1, the ACC now holds 4; if it was 0, the ACC holds 0. Compare and jump.
      CMP #4
      JPE Alarm     // True: jumps

    Masks to remember (8-bit ACC)

    • Test a bit: AND with a mask that has 1 in that bit, then CMP. For example, AND #1 keeps only the least significant bit, which is 1 for an odd number and 0 for an even number.
    • Set bits to 1 and keep the rest: OR with 1s in those bits. For example, OR #128 (OR B10000000) sets the most significant bit.
    • Clear bits to 0 and keep the rest: AND with 0s in those bits and 1s elsewhere. For example, AND B11110000 (AND &F0) clears bits 0 to 3 and keeps bits 4 to 7.
    • Toggle bits: XOR with 1s in those bits. XOR B11111111 flips every bit.
    • Clear the whole ACC: AND #0 (AND B00000000).

    Practise this: past-paper questions

    Show 32 more

    4.3 · 11 of 11Book page Contents

    Monitoring and controlling a device

    In a monitoring or control system, each bit of a memory location or register stands for one device: a sensor that is reporting (1 = triggered, 0 = not), or an actuator such as a light, motor or valve that is switched on (1) or off (0). One byte can hold the state of eight devices.

    • Monitoring: the program repeatedly reads the byte into the ACC and uses AND with a mask to test the bit for one sensor. A CMP and a conditional jump then decide what to do.
    • Control: the program sets a bit with OR and a mask to switch a device on, clears it with AND and an inverted mask to switch it off, or toggles it with XOR. It then stores the byte back so that the device changes.

    Using the lights register from earlier, where lights 3 and 4 (mask 00110000) are the outside lights:

    // are both outside lights off?
    LDD  Lights
    AND  B00110000
    CMP  #0
    JPE  BothOff
    
    // switch both outside lights on
    LDD  Lights
    OR   B00110000
    STO  Lights
    
    // switch both outside lights off
    LDD  Lights
    AND  B11001111
    STO  Lights

    How bit manipulation monitors or controls a device

    • Each bit in a register or memory location represents the state of one device (1 on, 0 off).
    • A mask picks out the bit or bits for the device.
    • AND with the mask tests (checks) whether a bit is set; AND with the inverted mask clears it (off).
    • OR with the mask sets the bit (on); XOR with the mask toggles it.
    • The value is read and tested repeatedly, and the new value is stored back to change the device.

    4.3

    Concepts in 4.3

    Every idea explained in 4.3, and where else in the notes it is used.

    ConceptIn one lineAlso used in
    arithmetic shiftA shift for signed numbers that preserves the sign bit; a right shift copies the sign bit in.only here
    bit manipulationChanging or testing individual bits using bitwise operations and shifts.4.3 Monitoring and controlling a device
    bit manipulation instructionsAND, OR, XOR, LSL and LSR: the instructions that work on the bits of the ACC.4.2 The instruction set and instruction groups
    bitwise ANDGives 1 only where both bits are 1; with a mask it checks or clears bits.4.3 Bit manipulation instructions
    bitwise NOTFlips every bit: 0 becomes 1 and 1 becomes 0.4.3 AND: checking and clearing bits
    bitwise ORGives 1 where either bit is 1; with a mask it sets bits to 1.4.3 Bit manipulation instructions
    bitwise XORGives 1 where exactly one bit is 1; with a mask it toggles bits.4.3 Bit manipulation instructions
    cyclic shiftA shift where bits leaving one end come back in at the other end.only here
    logical bitwise operationAn operation (AND, OR, XOR, NOT) applied to each bit, or pair of matching bits, of a binary string.4.1 The processor and the Arithmetic and Logic Unit (ALU), 4.3 Masks
    logical left shiftMoves bits left and puts 0s in on the right; each place multiplies by 2.4.3 Arithmetic shifts for signed numbers
    logical right shiftMoves bits right and puts 0s in on the left; each place divides by 2.4.3 Arithmetic shifts for signed numbers
    logical shiftMoves every bit left or right, discarding bits at one end and putting 0s in at the other.4.3 Arithmetic shifts for signed numbers
    maskA binary string used with a bitwise operator to pick out or change chosen bits.4.3 Logical bitwise operations, 4.3 AND: checking and clearing bits, 4.3 OR: setting bits, 4.3 XOR: toggling bits and clearing a register, 4.3 NOT: flipping every bit, 4.3 Bit manipulation instructions, 4.3 Monitoring and controlling a device
    zero flagA status register bit that shows whether a result was zero.only here

    When you want more than reading

    The notes are the front door. The class is where it clicks.

    Join a class →Ask on WhatsApp