Documentation

Complexity.Foundations.Data.Nat.Bits

Recovering a natural number from its binary digits #

The digits returned by Nat.bits are least significant first. Folding Nat.bit from the right with initial value zero reconstructs the original number.

Main results #

theorem Nat.bits_eq_nil_iff {n : ℕ} :
n.bits = [] ↔ n = 0

The binary digit list of n is empty if and only if n = 0.

theorem Nat.foldr_bit_ofFn {w : ℕ} (bits : Fin w → Bool) :
List.foldr bit 0 (List.ofFn bits) = ∑ k : Fin w, (bits k).toNat * 2 ^ ↑k

Reconstructing a finite little-endian word sums the digits' binary place values.

@[simp]
theorem Nat.foldr_bit_bits (n : ℕ) :

Folding Nat.bit over the binary representation of n gives back n.

theorem Nat.foldr_bit_lt (l : List Bool) :

A little-endian word represents a natural below the power of two given by its length.

Folding Nat.bit with base 1 over any word gives a positive number.

Folding Nat.bit with base 1 appends true to the binary representation.

theorem Nat.foldr_bit_one_lt (w : List Bool) :
List.foldr bit 1 w < 2 ^ (w.length + 1)

Folding Nat.bit with base 1 produces a number strictly below 2 ^ (|w| + 1).

theorem Nat.foldr_bit_testBit {n K : ℕ} (h : n.size ≤ K) :

Reading enough low-order digits recovers the natural, including its leading zero digits.

theorem Nat.size_div_two_add_one {n : ℕ} (hn : n ≠ 0) :
(n / 2).size + 1 = n.size

Halving a positive number removes one binary digit.