ITPEC FE Subject B April 2025 Question 6
ITPEC FE Subject B April 2025 — Question 6 of 20
Gray Code to Binary Conversion — fill in bitwise operations for iterative Gray-to-binary decoding.
The standard algorithm converts Gray code to binary by XOR-accumulating shifted copies of the input:
- Start with
y = xandz = x - In each iteration: shift
zright by 1, then XORywithz - Stop when
zbecomes00000000
This works because each binary bit equals the XOR of all Gray code bits from the MSB down to that position. The right-shift progressively brings higher bits into alignment for XOR.
Verification with GrayBiCon(00001100):
| Step | z (after `>>1`) | y (after `y ^ z`) |
|---|---|---|
| init | 00001100 | 00001100 |
| 1 | 00000110 | 00001010 |
| 2 | 00000011 | 00001001 |
| 3 | 00000001 | 00001000 |
| 4 | 00000000 | exit loop |
Result: 00001000 — matches the expected output.
Why not others:
- (a–c) use & (AND) for blank A — AND removes bits rather than shifting them; the algorithm needs to progressively shift z rightward
- (d–f) use << (left shift) for blank A — shifting left moves bits toward MSB, but the algorithm must scan from MSB to LSB
- (g) uses >> + y & z — AND masks bits instead of XOR-accumulating them
- (i) uses >> + y | z — OR sets bits but never clears them, producing incorrect results
Key rule: Gray-to-binary conversion uses y ^= (y >> n) pattern — right-shift to propagate MSB influence downward, XOR to accumulate.
AI-generated — may contain errors
The original exam layout is preserved in the image so diagrams, formulas, tables, and code remain accurate.
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.