Skip to main content

Module gray_code

Module gray_code 

Source
Expand description

Gray code generation

This module provides functionality for generating Gray codes, which are binary number systems where consecutive values differ by only one bit.

§Key Functions

  • brgc_gen - Generate binary reflected Gray code sequence

§Algorithm

The binary reflected Gray code (BRGC) is generated recursively. For n bits, the sequence is generated by: 1) recursively generating the (n-1)-bit sequence, 2) adding the highest-order bit (0) to those, 3) adding the highest-order bit (1) to the reversed (n-1)-bit sequence.

This produces a sequence of 2^n code words where each successive word differs by exactly one bit.

§Reference

Gray codes were originally designed by Frank Gray (1953) for pulse code modulation.

§Complexity

  • brgc_gen(n): O(2^n) output size, O(n) stack space

Functions§

brgc_gen
The brgc_gen function generates a binary reflexed gray code sequence of length n.