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 n returns a 2-tuple of type (Bool, Nat) where the Bool value indicates whether n is odd or not and the Nat value returns โŒŠn/2โŒ‹

Equations
    Instances For

      div2 n = โŒŠn/2โŒ‹ the greatest integer smaller than n/2

      Equations
        Instances For
          def Nat.bodd (n : โ„•) :

          bodd n returns true if n is odd

          Equations
            Instances For
              @[simp]
              theorem Nat.bodd_add (m n : โ„•) :
              (m + n).bodd = (m.bodd ^^ n.bodd)
              @[simp]
              theorem Nat.bodd_mul (m n : โ„•) :
              (m * n).bodd = (m.bodd && n.bodd)
              @[simp]
              theorem Nat.div2_succ (n : โ„•) :
              (n + 1).div2 = bif n.bodd then n.div2.succ else n.div2
              @[deprecated Nat.bit_bodd_div2 (since := "2025-09-24")]
              theorem Nat.bit_decomp (n : โ„•) :
              bit n.bodd n.div2 = n

              Alias of Nat.bit_bodd_div2.

              @[deprecated Nat.bit_false_zero (since := "2025-09-24")]
              theorem Nat.bit_zero :
              bit false 0 = 0

              Alias of Nat.bit_false_zero.

              def Nat.shiftLeft' (b : Bool) (m : โ„•) :

              shiftLeft' b m n performs a left shift of m n times and adds the bit b as the least significant bit each time. Returns the corresponding natural number

              Equations
                Instances For
                  @[simp]
                  theorem Nat.shiftLeft_eq' (m n : โ„•) :
                  m.shiftLeft n = m <<< n

                  Lean takes the unprimed name for Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n.

                  @[simp]
                  theorem Nat.shiftRight_eq (m n : โ„•) :
                  m.shiftRight n = m >>> n
                  def Nat.size :

                  size n : Returns the size of a natural number in bits i.e. the length of its binary representation

                  Equations
                    Instances For
                      def Nat.bits :
                      โ„• โ†’ List Bool

                      bits n returns a list of Bools which correspond to the binary representation of n, where the head of the list represents the least significant bit

                      Equations
                        Instances For
                          def Nat.ldiff :
                          โ„• โ†’ โ„• โ†’ โ„•

                          ldiff a b performs bitwise set difference. For each corresponding pair of bits taken as Booleans, say aแตข and bแตข, it applies the Boolean operation aแตข โˆง ยฌbแตข to obtain the iแต—สฐ bit of the result.

                          Equations
                            Instances For

                              bitwise ops

                              @[simp]
                              theorem Nat.bodd_bit (b : Bool) (n : โ„•) :
                              (bit b n).bodd = b
                              @[simp]
                              theorem Nat.div2_bit (b : Bool) (n : โ„•) :
                              (bit b n).div2 = n
                              theorem Nat.shiftLeft'_sub (b : Bool) (m : โ„•) {n k : โ„•} :
                              k โ‰ค n โ†’ shiftLeft' b m (n - k) = shiftLeft' b m n >>> k
                              theorem Nat.shiftLeft_sub (m : โ„•) {n k : โ„•} :
                              k โ‰ค n โ†’ m <<< (n - k) = m <<< n >>> k

                              boddDiv2_eq and bodd #

                              @[simp]
                              theorem Nat.div2_bit0 (n : โ„•) :
                              (2 * n).div2 = n

                              bit0 and bit1 #

                              theorem Nat.bit_add (b : Bool) (n m : โ„•) :
                              bit b (n + m) = bit false n + bit b m
                              theorem Nat.bit_add' (b : Bool) (n m : โ„•) :
                              bit b (n + m) = bit b n + bit false m
                              theorem Nat.bit_ne_zero (b : Bool) {n : โ„•} (h : n โ‰  0) :
                              bit b n โ‰  0
                              @[simp]
                              theorem Nat.bitCasesOn_bit0 {motive : โ„• โ†’ Sort u} (H : (b : Bool) โ†’ (n : โ„•) โ†’ motive (bit b n)) (n : โ„•) :
                              bitCasesOn (2 * n) H = H false n
                              @[simp]
                              theorem Nat.bitCasesOn_bit1 {motive : โ„• โ†’ Sort u} (H : (b : Bool) โ†’ (n : โ„•) โ†’ motive (bit b n)) (n : โ„•) :
                              bitCasesOn (2 * n + 1) H = H true n
                              theorem Nat.bit_cases_on_injective {motive : โ„• โ†’ Sort u} :
                              Function.Injective fun (H : (b : Bool) โ†’ (n : โ„•) โ†’ motive (bit b n)) (n : โ„•) => bitCasesOn n H
                              @[simp]
                              theorem Nat.bit_cases_on_inj {motive : โ„• โ†’ Sort u} (Hโ‚ Hโ‚‚ : (b : Bool) โ†’ (n : โ„•) โ†’ motive (bit b n)) :
                              ((fun (n : โ„•) => bitCasesOn n Hโ‚) = fun (n : โ„•) => bitCasesOn n Hโ‚‚) โ†” Hโ‚ = Hโ‚‚
                              theorem Nat.bit_le (b : Bool) {m n : โ„•} :
                              m โ‰ค n โ†’ bit b m โ‰ค bit b n
                              theorem Nat.bit_lt_bit {m n : โ„•} (a b : Bool) (h : m < n) :
                              bit a m < bit b n
                              @[simp]
                              theorem Nat.bits_append_bit (n : โ„•) (b : Bool) (hn : n = 0 โ†’ b = true) :
                              (bit b n).bits = b :: n.bits
                              @[simp]
                              theorem Nat.bit0_bits (n : โ„•) (hn : n โ‰  0) :
                              (2 * n).bits = false :: n.bits
                              @[simp]
                              theorem Nat.bit1_bits (n : โ„•) :
                              (2 * n + 1).bits = true :: n.bits