Binary digits by repeated halving #
Halving a number n repeatedly and recording the parity of each nonzero intermediate value yields
the little-endian binary digits n.bits. A machine that halves a unary counter once per round and
emits one bit per round therefore writes n in binary, and stops emitting once the counter reaches
zero, so any number of rounds at least n suffices.
Main results #
Nat.bits_of_pos: the binary digits of a positive number are its parity followed by the binary digits of its half.Nat.iterate_div: dividing bybitimes divides byb ^ i.Nat.flatten_map_range_halfBit: forn ≤ m, emitting the parity of every nonzero value among the firstmhalvings ofnproducesn.bits.
The bit a halving machine emits at the value k: nothing at zero, else the parity of k.
Instances For
Binary digits by repeated halving. If n ≤ m, emitting halfBit of each of the first m
values n, n / 2, n / 4, … writes the little-endian binary digits of n.