Documentation

CslibFrontier.Foundations.Data.Nat.Bitwise.CommonPrefix

Common binary prefixes through exclusive or #

Two naturals A < B agree above bit s = (A ^^^ B).size, i.e. on the common prefix of their binary expansions, and the dyadic point (2 (A / 2^s) + 1) 2^(s-1), the common prefix followed by a 1, separates them. This is the arithmetic content of the compression code in the completeness proof of Bogdanov and Trevisan, Claim 20.

Main results #

References #

theorem Nat.div_two_pow_eq_iff_xor_lt (A B k : ℕ) :
A / 2 ^ k = B / 2 ^ k ↔ A ^^^ B < 2 ^ k

Two naturals have the same quotient by 2 ^ k exactly when their exclusive or is below 2 ^ k.

theorem Nat.lt_commonPrefix_mid_le {A B : ℕ} (hAB : A < B) :
1 ≤ (A ^^^ B).size ∧ A / 2 ^ (A ^^^ B).size = B / 2 ^ (A ^^^ B).size ∧ A < (2 * (A / 2 ^ (A ^^^ B).size) + 1) * 2 ^ ((A ^^^ B).size - 1) ∧ (2 * (A / 2 ^ (A ^^^ B).size) + 1) * 2 ^ ((A ^^^ B).size - 1) ≤ B

Splitting at the highest differing bit. For A < B and s = (A ^^^ B).size, the two numbers have the same quotient Z by 2 ^ s, and the dyadic point (2 Z + 1) 2^(s-1) lies strictly above A and at most B.