Skip to main content

Module bits

Module bits 

Source
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_bits bits of bits, each the bits_to_index of 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_bits bits of index, the most significant first (the bits above $2^{n}$ are dropped).