This example generates a 4-bit Gray code. A Gray code is a sequence of binary states where neighboring states differ by exactly one bit.
This is useful in hardware, sensors, and encoders because changing one bit at a time reduces ambiguity during transitions.
The program counts from 0 to 15 and maps each integer n to n xor (n >> 1). That formula creates the reflected binary Gray-code sequence.
After generating the sequence, it checks each neighboring pair, including the transition from the final state back to the first state.
The output reports 16 visited states and 16 unique states, so the sequence covers the whole 4-bit state space. The maximum adjacent Hamming distance is 1, meaning no transition changes more than one bit.
The trust gate verifies the bit width, sequence length, uniqueness, cyclic wraparound behavior, and one-bit-transition property. These checks catch duplicate states and incorrect binary transformations.
From the repository root:
node examples/gray_code_counter.js