Processor Fundamentals and Assembly Language: Question 6
Syllabus 4.3
An embedded control register stores eight status flags, one per bit, numbered bit 0 (least significant) to bit 7 (most significant). A programmer wants to test whether bit 4 of this register is set to 1, without changing the contents of the register itself.
Which mask and operation correctly tests whether bit 4 is set to 1?
Show worked solution Hide worked solution
Worked solution
Why AND with 00010000 tests bit 4
Bit 4 has place value 2^4 = 16, so the mask that isolates only bit 4 is 00010000. ANDing the register with this mask keeps bit 4 exactly as it was (since any bit AND 1 = that bit) and forces every other bit to 0 (since any bit AND 0 = 0), because the mask has a 0 in every other position.
The result of the AND is therefore either 00010000 (16, non-zero) if bit 4 of the register was 1, or 00000000 (0) if bit 4 was 0. Checking whether the result is not equal to zero correctly reveals bit 4’s original value, and the AND operation itself does not alter the register. It only produces a separate result used for the test.
Why the other options are wrong
- B (OR with 00010000): ORing with a mask bit of 1 always sets that bit to 1 in the result (any bit OR 1 = 1), no matter what bit 4 originally held. The result of this OR is always non-zero, so it can never distinguish bit 4 = 0 from bit 4 = 1.
- C (AND with 11101111, check for zero): this mask clears bit 4 and keeps every other bit unchanged. A zero result here depends on all seven other bits being 0 as well, not on bit 4 at all, so it does not correctly isolate or test bit 4.
- D (XOR with 00010000, compare to original): XORing with a mask bit of 1 always flips that bit (any bit XOR 1 = its opposite), so the result can never equal the original register value. This comparison is never true regardless of bit 4’s original state.
Final answer
A, AND the register with 00010000 and check whether the result is non-zero; this isolates bit 4 without changing the register, giving a non-zero result exactly when bit 4 is set to 1.