Documentation

Mathlib.Data.Nat.Bitwise

Bitwise operations on natural numbers #

In the first half of this file, we provide theorems for reasoning about natural numbers from their bitwise properties. In the second half of this file, we show properties of the bitwise operations lor', land' and lxor', which are defined in core.

Main results #

Future work #

There is another way to express bitwise properties of natural number: digits 2. The two ways should be connected.

Keywords #

bitwise, and, or, xor

@[simp]
@[simp]
theorem Nat.bit_true :
@[simp]
theorem Nat.bit_eq_zero {n : } {b : Bool} :
Nat.bit b n = 0 n = 0 b = false
theorem Nat.zero_of_testBit_eq_false {n : } (h : ∀ (i : ), Nat.testBit n i = false) :
n = 0
@[simp]
theorem Nat.testBit_eq_inth (n : ) (i : ) :

The ith bit is the ith element of n.bits.

theorem Nat.eq_of_testBit_eq {n : } {m : } (h : ∀ (i : ), Nat.testBit n i = Nat.testBit m i) :
n = m

Bitwise extensionality: Two numbers agree if they agree at every bit position.

theorem Nat.exists_most_significant_bit {n : } (h : n 0) :
i, Nat.testBit n i = true ∀ (j : ), i < jNat.testBit n j = false
theorem Nat.lt_of_testBit {n : } {m : } (i : ) (hn : Nat.testBit n i = false) (hm : Nat.testBit m i = true) (hnm : ∀ (j : ), i < jNat.testBit n j = Nat.testBit m j) :
n < m
@[simp]
theorem Nat.testBit_two_pow_of_ne {n : } {m : } (hm : n m) :
theorem Nat.testBit_two_pow (n : ) (m : ) :
(Nat.testBit (2 ^ n) m = true) = (n = m)
theorem Nat.bitwise'_comm {f : BoolBoolBool} (hf : ∀ (b b' : Bool), f b b' = f b' b) (hf' : f false false = false) (n : ) (m : ) :

If f is a commutative operation on bools such that f false false = false, then bitwise f is also commutative.

theorem Nat.lor'_comm (n : ) (m : ) :
theorem Nat.land'_comm (n : ) (m : ) :
theorem Nat.lxor'_comm (n : ) (m : ) :
@[simp]
theorem Nat.zero_lxor' (n : ) :
Nat.lxor' 0 n = n
@[simp]
theorem Nat.lxor'_zero (n : ) :
Nat.lxor' n 0 = n
@[simp]
theorem Nat.zero_land' (n : ) :
Nat.land' 0 n = 0
@[simp]
theorem Nat.land'_zero (n : ) :
Nat.land' n 0 = 0
@[simp]
theorem Nat.zero_lor' (n : ) :
Nat.lor' 0 n = n
@[simp]
theorem Nat.lor'_zero (n : ) :
Nat.lor' n 0 = n

Proving associativity of bitwise operations in general essentially boils down to a huge case distinction, so it is shorter to use this tactic instead of proving it in the general case.

Equations
Instances For
    theorem Nat.lxor'_assoc (n : ) (m : ) (k : ) :
    theorem Nat.land'_assoc (n : ) (m : ) (k : ) :
    theorem Nat.lor'_assoc (n : ) (m : ) (k : ) :
    @[simp]
    theorem Nat.lxor'_self (n : ) :
    Nat.lxor' n n = 0
    theorem Nat.lxor_cancel_right (n : ) (m : ) :
    theorem Nat.lxor'_cancel_left (n : ) (m : ) :
    @[simp]
    theorem Nat.lxor'_right_inj {n : } {m : } {m' : } :
    Nat.lxor' n m = Nat.lxor' n m' m = m'
    @[simp]
    theorem Nat.lxor'_left_inj {n : } {m : } {m' : } :
    Nat.lxor' m n = Nat.lxor' m' n m = m'
    @[simp]
    theorem Nat.lxor'_eq_zero {n : } {m : } :
    Nat.lxor' n m = 0 n = m
    theorem Nat.lxor'_ne_zero {n : } {m : } :
    Nat.lxor' n m 0 n m
    theorem Nat.lxor'_trichotomy {a : } {b : } {c : } (h : a Nat.lxor' b c) :
    Nat.lxor' b c < a Nat.lxor' a c < b Nat.lxor' a b < c
    theorem Nat.lt_lxor'_cases {a : } {b : } {c : } (h : a < Nat.lxor' b c) :
    Nat.lxor' a c < b Nat.lxor' a b < c