Skip to content

9618 · 4.3

Bit manipulation — practice questions

Practice and worked examples for 9618 Bit manipulation. Short previews only — attempt the full question in MarkScheme against the official scheme.

Worked example 1

An 8-bit register contains the binary value 00110101. What is the result, in binary and denary, after applying: a) LSL 2, b) LSR 3?

Show solution outline

The original value 00110101 is 32+16+4+1=5332 + 16 + 4 + 1 = 53 in denary.

a) LSL 2 (Logical Shift Left by 2):

  • Original: 00110101
  • Shift 1: 01101010 (The leading 0 is discarded, a 0 is added at the end)
  • Shift 2: 11010100 (The leading 0 is discarded, a 0 is added at the end)
  • Result (Binary): 11010100
  • Result (Denary): 128+64+16+4=212128 + 64 + 16 + 4 = 212.
  • Check: 53×22=53×4=21253 \times 2^2 = 53 \times 4 = 212. The calculation is correct.

b) LSR 3 (Logical Shift Right by 3):

  • Original: 00110101
  • Shift 1: 00011010 (The trailing 1 is discarded, a 0 is added at the start)
  • Shift 2: 00001101 (The trailing 0 is discarded, a 0 is added at the start)
  • Shift 3: 00000110 (The trailing 1 is discarded, a 0 is added at the start)
  • Result (Binary): 00000110
  • Result (Denary): 4+2=64 + 2 = 6.
  • Check: 53÷23=53÷8=653 \div 2^3 = 53 \div 8 = 6 with a remainder of 5. This is correct for integer division.

Worked example 2

An 8-bit register stores a signed integer in two's complement. It holds the value 11101100. What is the result of applying an ASR 2? Give your answer in binary and state the denary equivalent before and after.

Show solution outline

The original value is 11101100.

  • As it's a negative number (MSB is 1), we find its magnitude by flipping the bits and adding 1: 00010011+1=0001010000010011 + 1 = 00010100. This is 16+4=2016 + 4 = 20. So the original value is -20.

Applying ASR 2 (Arithmetic Shift Right by 2):

  • The sign bit is 1.
  • Original: 11101100
  • Shift 1: The LSB (0) is discarded. The sign bit (1) is copied into the MSB position. Result: 11110110.
  • Shift 2: The new LSB (0) is discarded. The sign bit (1) is copied again. Result: 11111011.
  • Result (Binary): 11111011

Denary value of the result:

  • The result 11111011 is negative. To find its magnitude, flip the bits and add 1: 00000100+1=0000010100000100 + 1 = 00000101. This is 5.
  • So the final denary value is -5.
  • Check: 20÷22=20÷4=5-20 \div 2^2 = -20 \div 4 = -5. The arithmetic shift has correctly preserved the sign during division.

Worked example 3

A byte variable, STATUS, holds the value 10010110. Bits represent flags (from right, bit 0): Bit 1: READONLYREAD_ONLY, Bit 4: ERROR, Bit 7: ENABLED. Perform the following operations, showing the mask and the result in binary. a) Clear the ERROR flag (set bit 4 to 0). b) Set the ENABLED flag (set bit 7 to 1). c) Toggle the READONLYREAD_ONLY flag (flip bit 1).

Show solution outline

Original STATUS byte: 10010110

a) Clear the ERROR flag (bit 4):

  • We need to AND the byte with a mask that has a 0 at bit 4 and 1s everywhere else.
  • Mask: 11101111
  • Operation: 10010110 AND 11101111
  10010110 (STATUS)
& 11101111 (Mask)
----------
  10000110 (Result)
  • Result: 10000110

b) Set the ENABLED flag (bit 7):

  • We need to OR the byte with a mask that has a 1 at bit 7 and 0s everywhere else.
  • Mask: 10000000
  • Operation: 10010110 OR 10000000
  10010110 (STATUS)
| 10000000 (Mask)
----------
  10010110 (Result)
  • Note: Bit 7 was already 1, so the OR operation leaves it as 1. If it had been 0, it would have been set to 1.
  • Result: 10010110

c) Toggle the READ_ONLY flag (bit 1):

  • We need to XOR the byte with a mask that has a 1 at bit 1 and 0s everywhere else.
  • Mask: 00000010
  • Operation: 10010110 XOR 00000010
  10010110 (STATUS)
^ 00000010 (Mask)
----------
  10010100 (Result)
  • Bit 1 was 1, and 1XOR1=01 XOR 1 = 0, so it has been flipped to 0.
  • Result: 10010100