Expand description
Integers as bit strings: the reflected binary Gray code and the conversions between an index and its bits, most significant bit first.
§Model
The binary reflected Gray code of $i$ and its inverse are
$$ g(i) = i \oplus \lfloor i / 2 \rfloor, \qquad g^{-1}(g) = \bigoplus_{k \ge 0} \left\lfloor g / 2^{k} \right\rfloor, $$
so that $g(i)$ and $g(i + 1)$ differ in one bit. An index $i$ of $n$ bits is written $b_{0} b_{1} \dots b_{n-1}$ with $i = \sum_{k=0}^{n-1} b_{k} 2^{n-1-k}$ (the most significant bit first, the order of the labels of every constellation and of the symbols of every code of comnum).
§Examples
use comnum_math::{bits_to_index, gray, gray_inverse, index_to_bits};
assert_eq!(gray(5), 7); // 101 → 111
assert_eq!(gray_inverse(7), 5);
assert_eq!(bits_to_index(&[1, 0, 1, 1]), 11);
assert_eq!(index_to_bits(11, 4).collect::<Vec<u8>>(), [1, 0, 1, 1]);Functions§
- bits_
to_ index - The index $i = \sum_{k} b_{k} 2^{n-1-k}$ of the bits $b_{0} \dots b_{n-1}$, the most significant first (only the lowest bit of every value is read).
- bits_
to_ indices - The indices of the blocks of
n_bitsbits ofbits, each thebits_to_indexof its block (the labels of the symbols of a bit stream, the symbols of a code over $\mathrm{GF}(2^{m})$). - gray
- The binary reflected Gray code $g(i) = i \oplus \lfloor i/2 \rfloor$.
- gray_
inverse - The inverse of
gray: $g^{-1}(g) = \bigoplus_{k \ge 0} \lfloor g / 2^{k} \rfloor$. - index_
to_ bits - The
n_bitsbits ofindex, the most significant first (the bits above $2^{n}$ are dropped).