Processor Fundamentals and Assembly Language: Question 8
Syllabus 4.3
A register named REG in a data-logging device starts with the 8-bit value 10110010, where each bit records the on/off state of a separate sensor. Each part below uses a different bitwise mask on the original, unchanged value of REG (10110010) to carry out a different task.
(a) REG is ANDed with the mask 00100000. State the 8-bit binary result, and use it to state whether bit 5 of REG is set to 1 or set to 0. [2]
(b) REG is ORed with the mask 00000100, so that bit 2 is forced to 1 without disturbing any other bit. State the 8-bit binary result, and explain why every bit of REG other than bit 2 is guaranteed to be unchanged by this operation. [2]
(c) REG is XORed with the mask 00000001. State the 8-bit binary result, and state the general effect that XORing any single bit with a mask bit of 1 has on that bit. [2]
Show worked solution Hide worked solution
Worked solution
Part (a): Testing bit 5 with AND
Line up REG and the mask, then AND bit by bit (result bit is 1 only where both input bits are 1):
REG: 1 0 1 1 0 0 1 0
Mask: 0 0 1 0 0 0 0 0
AND: 0 0 1 0 0 0 0 0
Result: 00100000. Since the mask is 0 everywhere except bit 5, every bit of the AND result other than bit 5 is forced to 0 (X AND 0 = 0), while bit 5 of the result equals bit 5 of REG (X AND 1 = X). The result 00100000 is non-zero, so bit 5 of REG is set to 1.
[2 marks]: 1 for the correct AND result 00100000, 1 for correctly concluding bit 5 is set to 1.
Part (b): Setting bit 2 with OR
Line up REG and the mask, then OR bit by bit (result bit is 1 if either input bit is 1):
REG: 1 0 1 1 0 0 1 0
Mask: 0 0 0 0 0 1 0 0
OR: 1 0 1 1 0 1 1 0
Result: 10110110.
Every bit of REG other than bit 2 is guaranteed to be unchanged because, for any bit X, X OR 0 = X. The mask holds 0 in every position except bit 2, so those positions simply copy REG’s original bits straight through. Only bit 2, where the mask holds 1, is forced to 1 regardless of its original value (X OR 1 = 1).
[2 marks]: 1 for the correct OR result 10110110, 1 for correctly explaining X OR 0 = X as the reason the other bits are preserved.
Part (c): Toggling bit 0 with XOR
Line up the original REG (10110010) and the mask, then XOR bit by bit (result bit is 1 if the two input bits differ):
REG: 1 0 1 1 0 0 1 0
Mask: 0 0 0 0 0 0 0 1
XOR: 1 0 1 1 0 0 1 1
Result: 10110011.
In general, XORing any single bit with a mask bit of 1 toggles (inverts) that bit: 0 becomes 1, and 1 becomes 0. A mask bit of 0 leaves the corresponding bit unchanged (X XOR 0 = X).
[2 marks]: 1 for the correct XOR result 10110011, 1 for correctly describing the toggling effect of XORing with a mask bit of 1.
Final answers
- (a) 00100000; bit 5 of REG is set to 1.
- (b) 10110110; unaffected bits are preserved because X OR 0 = X.
- (c) 10110011; XOR with a mask bit of 1 toggles that bit.