ITPEC FE Subject B April 2025 Question 6

Source exam: ITPEC FE Subject B April 2025Topic: Array Manipulation

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:

  1. Start with y = x and z = x
  2. In each iteration: shift z right by 1, then XOR y with z
  3. Stop when z becomes 00000000

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):

Stepz (after `>>1`)y (after `y ^ z`)
init0000110000001100
10000011000001010
20000001100001001
30000000100001000
400000000exit 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.