# Documentation

Mathlib.Data.Nat.Bits

# Additional properties of binary recursion on Nat#

This file documents additional properties of binary recursion, which allows us to more easily work with operations which do depend on the number of leading zeros in the binary representation of n. For example, we can more easily work with Nat.bits and Nat.size.

See also: Nat.bitwise, Nat.pow (for various lemmas about size and shiftLeft/shiftRight), and Nat.digits.

### boddDiv2_eq and bodd#

@[simp]
theorem Nat.boddDiv2_eq (n : ) :
= (, )
@[simp]
theorem Nat.bodd_bit0 (n : ) :
@[simp]
theorem Nat.bodd_bit1 (n : ) :
@[simp]
theorem Nat.div2_bit0 (n : ) :
@[simp]
theorem Nat.div2_bit1 (n : ) :

### bit0 and bit1#

@[simp]
theorem Nat.bit0_eq_bit0 {m : } {n : } :
bit0 m = bit0 n m = n
@[simp]
theorem Nat.bit1_eq_bit1 {m : } {n : } :
bit1 m = bit1 n m = n
@[simp]
theorem Nat.bit1_eq_one {n : } :
bit1 n = 1 n = 0
@[simp]
theorem Nat.one_eq_bit1 {n : } :
1 = bit1 n n = 0
theorem Nat.bit_add (b : Bool) (n : ) (m : ) :
Nat.bit b (n + m) = + Nat.bit b m
theorem Nat.bit_add' (b : Bool) (n : ) (m : ) :
Nat.bit b (n + m) = Nat.bit b n +
theorem Nat.bit_ne_zero (b : Bool) {n : } (h : n 0) :
Nat.bit b n 0
theorem Nat.bit0_mod_two {n : } :
bit0 n % 2 = 0
theorem Nat.bit1_mod_two {n : } :
bit1 n % 2 = 1
theorem Nat.pos_of_bit0_pos {n : } (h : 0 < bit0 n) :
0 < n
@[simp]
theorem Nat.bitCasesOn_bit {C : Sort u} (H : (b : Bool) → (n : ) → C (Nat.bit b n)) (b : Bool) (n : ) :
Nat.bitCasesOn (Nat.bit b n) H = H b n
@[simp]
theorem Nat.bitCasesOn_bit0 {C : Sort u} (H : (b : Bool) → (n : ) → C (Nat.bit b n)) (n : ) :
@[simp]
theorem Nat.bitCasesOn_bit1 {C : Sort u} (H : (b : Bool) → (n : ) → C (Nat.bit b n)) (n : ) :
@[simp]
theorem Nat.bit_cases_on_inj {C : Sort u} (H₁ : (b : Bool) → (n : ) → C (Nat.bit b n)) (H₂ : (b : Bool) → (n : ) → C (Nat.bit b n)) :
((fun n => ) = fun n => ) H₁ = H₂
theorem Nat.bit0_eq_zero {n : } :
bit0 n = 0 n = 0
theorem Nat.bit_eq_zero_iff {n : } {b : Bool} :
Nat.bit b n = 0 n = 0
theorem Nat.binaryRec_eq' {C : Sort u_1} {z : C 0} {f : (b : Bool) → (n : ) → C nC (Nat.bit b n)} (b : Bool) (n : ) (h : f false 0 z = z (n = 0)) :
Nat.binaryRec z f (Nat.bit b n) = f b n ()

The same as binaryRec_eq, but that one unfortunately requires f to be the identity when appending false to 0. Here, we allow you to explicitly say that that case is not happening, i.e. supplying n = 0 → b = true.

def Nat.binaryRec' {C : Sort u_1} (z : C 0) (f : (b : Bool) → (n : ) → (n = 0) → C nC (Nat.bit b n)) (n : ) :
C n

The same as binaryRec, but the induction step can assume that if n=0, the bit being appended is true

Instances For
def Nat.binaryRecFromOne {C : Sort u_1} (z₀ : C 0) (z₁ : C 1) (f : (b : Bool) → (n : ) → n 0C nC (Nat.bit b n)) (n : ) :
C n

The same as binaryRec, but special casing both 0 and 1 as base cases

Instances For
@[simp]
theorem Nat.zero_bits :
= []
@[simp]
theorem Nat.bits_append_bit (n : ) (b : Bool) (hn : n = 0) :
Nat.bits (Nat.bit b n) = b ::
@[simp]
theorem Nat.bit0_bits (n : ) (hn : n 0) :
@[simp]
theorem Nat.bit1_bits (n : ) :
@[simp]
theorem Nat.one_bits :
= [true]