Show understanding of how bit manipulation can be used to monitor/control a device

Published by Patrick Mutisya · 8 days ago

Cambridge A-Level Computer Science 9618 – Bit Manipulation

4.3 Bit Manipulation

Learning Objective

Show understanding of how bit manipulation can be used to monitor and control a device.

Why Use Bits?

In embedded systems and hardware interfacing a single byte (8 bits) can represent up to 256 different states. By treating each bit as a flag we can:

  • Read the status of multiple sensors with a single read operation.
  • Set or clear control signals without affecting other bits.
  • Minimise memory usage and improve execution speed.

Common Bitwise Operators (Python / C style)

OperatorNameEffect
&ANDResult bit is 1 only if both operands are 1.
|ORResult bit is 1 if either operand is 1.
^XORResult bit is 1 if operands differ.
~NOTFlips every bit (two's complement).
<<Left shiftMoves bits left, inserting zeros on the right.
>>Right shiftMoves bits right, discarding low‑order bits.

Representing Device I/O in a Byte

Consider an 8‑bit register PORTA that controls a set of LEDs and reads a set of switches.

Bit 7 6 5 4 3 2 1 0

| | | | | | | +--- Switch 0 (input)

| | | | | | +----- Switch 1 (input)

| | | | | +------- LED 0 (output)

| | | | +--------- LED 1 (output)

| | | +----------- Unused

| | +------------- Unused

| +--------------- Unused

+----------------- Unused

Creating Bit Masks

Bit masks isolate or modify specific bits. The mask for LED 0 (bit 2) is \$1 \ll 2 = 0b00000100 = 0x04\$.

Typical masks for the example above:

NameBit PositionMask (binary)Mask (hex)
LED020b000001000x04
LED130b000010000x08
SW000b000000010x01
SW110b000000100x02

Reading Device Status

To test whether Switch 1 is ON:

\$\text{status} = \text{PORTA} \; \&\; 0x02\$

If status is non‑zero, the switch is high; otherwise it is low.

Setting and Clearing Output Bits

Turn LED 0 ON (set bit 2):

\$\text{PORTA} = \text{PORTA} \;|\; 0x04\$

Turn LED 0 OFF (clear bit 2):

\$\text{PORTA} = \text{PORTA} \;\&\; \sim0x04\$

Toggle LED 1 (invert bit 3):

\$\text{PORTA} = \text{PORTA} \; \hat{}\; 0x08\$

Practical Example: Temperature Sensor Monitoring

Assume a microcontroller reads an 8‑bit status register SENSOR_STATUS where:

  • Bit 7 – Over‑temperature alarm
  • Bit 6 – Sensor fault
  • Bits 5‑0 – Reserved

The following pseudo‑code demonstrates how to react to the alarm:

if (SENSOR_STATUS & 0x80) != 0: # Over‑temperature?

activatecoolingfan() # set control bit

else:

deactivatecoolingfan()

Here 0x80 = 1 << 7 isolates the alarm flag.

Combining Multiple Flags

Sometimes several conditions must be met before an action is taken. Using logical AND on masked bits achieves this:

\$\text{critical} = (\text{SENSOR_STATUS} \& 0xC0) == 0xC0\$

Explanation: 0xC0 = 0b11000000 masks bits 7 and 6. The expression is true only when both the alarm and fault bits are set.

Summary Checklist

  1. Identify which bits correspond to which device signals.
  2. Create masks using left‑shift (\$1 \ll n\$) or hexadecimal literals.
  3. Use & to read a flag, | to set, &~ to clear, and ^ to toggle.
  4. Combine masks with logical operators for complex conditions.
  5. Always preserve unrelated bits by masking before modifying.

Suggested diagram: A byte diagram showing bits labelled LED0, LED1, SW0, SW1 and arrows indicating read/write operations.