Mathlib.Data.Nat.Bitwise
39 declarations. Mathlib docs · Source
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 xor, which are defined in core.
Main results
eq_of_testBit_eq: two natural numbers are equal if they have equal bits at every position.exists_most_significant_bit: ifn ≠ 0, then there is some positionithat contains the most significant1-bit ofn.lt_of_testBit: ifnandmare numbers andiis a position such that thei-th bit ofnis zero, thei-th bit ofmis one, and all more significant bits are equal, thenn < m.
TODO
There is another way to express bitwise properties of natural number: digits 2. The two ways
should be connected.
Tags
bitwise, and, or, xor
theorem Nat.bitwise_zero_left E0 #
∀ {f : Bool → Bool → Bool} (m : ℕ), Nat.bitwise f 0 m = if f false true = true then m else 0
theorem Nat.bitwise_zero_right E0 #
∀ {f : Bool → Bool → Bool} (n : ℕ), Nat.bitwise f n 0 = if f true false = true then n else 0
theorem Nat.bitwise_zero E1 #
∀ {f : Bool → Bool → Bool}, Nat.bitwise f 0 0 = 0
theorem Nat.bitwise_of_ne_zero E1 #
∀ {f : Bool → Bool → Bool} {n m : ℕ},
n ≠ 0 → m ≠ 0 → Nat.bitwise f n m = Nat.bit (f n.bodd m.bodd) (Nat.bitwise f (n / 2) (m / 2))
theorem Nat.binaryRec_of_ne_zero E1 #
∀ {C : ℕ → Sort u_1} (z : C 0) (f : (b : Bool) → (n : ℕ) → C n → C (Nat.bit b n)) {n : ℕ},
n ≠ 0 → Nat.binaryRec z f n = ⋯ ▸ f n.bodd n.div2 (Nat.binaryRec z f n.div2)
theorem Nat.bitwise_bit E0 #
∀ {f : Bool → Bool → Bool},
autoParam (f false false = false) Nat.bitwise_bit._auto_1 →
∀ (a : Bool) (m : ℕ) (b : Bool) (n : ℕ),
Nat.bitwise f (Nat.bit a m) (Nat.bit b n) = Nat.bit (f a b) (Nat.bitwise f m n)
theorem Nat.bit_mod_two_eq_zero_iff E0 #
∀ (a : Bool) (x : ℕ), Nat.bit a x % 2 = 0 ↔ (!a) = true
theorem Nat.bit_mod_two_eq_one_iff E0 #
∀ (a : Bool) (x : ℕ), Nat.bit a x % 2 = 1 ↔ a = true
theorem Nat.lor_bit E0 #
∀ (a : Bool) (m : ℕ) (b : Bool) (n : ℕ), Nat.bit a m ||| Nat.bit b n = Nat.bit (a || b) (m ||| n)
theorem Nat.land_bit E0 #
∀ (a : Bool) (m : ℕ) (b : Bool) (n : ℕ), Nat.bit a m &&& Nat.bit b n = Nat.bit (a && b) (m &&& n)
theorem Nat.ldiff_bit E0 #
∀ (a : Bool) (m : ℕ) (b : Bool) (n : ℕ), (Nat.bit a m).ldiff (Nat.bit b n) = Nat.bit (a && !b) (m.ldiff n)
theorem Nat.xor_bit E0 #
∀ (a : Bool) (m : ℕ) (b : Bool) (n : ℕ), Nat.bit a m ^^^ Nat.bit b n = Nat.bit (a != b) (m ^^^ n)
theorem Nat.testBit_lor E1 #
∀ (m n k : ℕ), (m ||| n).testBit k = (m.testBit k || n.testBit k)
theorem Nat.testBit_land E1 #
∀ (m n k : ℕ), (m &&& n).testBit k = (m.testBit k && n.testBit k)
theorem Nat.testBit_ldiff E0 #
∀ (m n k : ℕ), (m.ldiff n).testBit k = (m.testBit k && !n.testBit k)
theorem Nat.bitwise_bit' E1 #
∀ {f : Bool → Bool → Bool} (a : Bool) (m : ℕ) (b : Bool) (n : ℕ),
(m = 0 → a = true) →
(n = 0 → b = true) → Nat.bitwise f (Nat.bit a m) (Nat.bit b n) = Nat.bit (f a b) (Nat.bitwise f m n)
An alternative for bitwise_bit which replaces the f false false = false assumption
with assumptions that neither bit a m nor bit b n are 0
(albeit, phrased as the implications m = 0 → a = true and n = 0 → b = true)
theorem Nat.bitwise_eq_binaryRec E1 #
∀ (f : Bool → Bool → Bool),
Nat.bitwise f = fun n ↦
Nat.binaryRec (motive := fun x ↦ ℕ → ℕ) (fun n ↦ bif f false true then n else 0)
(fun a m Ia n ↦ Nat.binaryRec (bif f true false then Nat.bit a m else 0) (fun b n x ↦ Nat.bit (f a b) (Ia n)) n) n
theorem Nat.zero_of_testBit_eq_false E1 #
∀ {n : ℕ}, (∀ (i : ℕ), n.testBit i = false) → n = 0
theorem Nat.testBit_eq_false_of_lt E1 #
∀ {n i : ℕ}, n < 2 ^ i → n.testBit i = false
theorem Nat.testBit_eq_inth E1 #
∀ (n i : ℕ), n.testBit i = n.bits.getI i
The ith bit is the ith element of n.bits.
theorem Nat.exists_most_significant_bit E3 #
∀ {n : ℕ}, n ≠ 0 → ∃ i, n.testBit i = true ∧ ∀ (j : ℕ), i < j → n.testBit j = false
theorem Nat.lt_of_testBit E3 #
∀ {n m : ℕ} (i : ℕ), n.testBit i = false → m.testBit i = true → (∀ (j : ℕ), i < j → n.testBit j = m.testBit j) → n < m
theorem Nat.bitwise_swap E1 #
∀ {f : Bool → Bool → Bool}, Nat.bitwise (Function.swap f) = Function.swap (Nat.bitwise f)
theorem Nat.bitwise_comm E1 #
∀ {f : Bool → Bool → Bool}, (∀ (b b' : Bool), f b b' = f b' b) → ∀ (n m : ℕ), Nat.bitwise f n m = Nat.bitwise f m n
If f is a commutative operation on bools such that f false false = false, then bitwise f
is also commutative.
theorem Nat.lor_comm E1 #
∀ (n m : ℕ), n ||| m = m ||| n
theorem Nat.land_comm E1 #
∀ (n m : ℕ), n &&& m = m &&& n
theorem Nat.and_two_pow E1 #
∀ (n i : ℕ), n &&& 2 ^ i = (n.testBit i).toNat * 2 ^ i
theorem Nat.two_pow_and E1 #
∀ (n i : ℕ), 2 ^ i &&& n = 2 ^ i * (n.testBit i).toNat
theorem Nat.land_assoc E1 #
∀ (n m k : ℕ), n &&& m &&& k = n &&& (m &&& k)
theorem Nat.lor_assoc E1 #
∀ (n m k : ℕ), n ||| m ||| k = n ||| (m ||| k)
theorem Nat.xor_trichotomy E1 #
∀ {a b c : ℕ}, a ^^^ b ^^^ c ≠ 0 → b ^^^ c < a ∨ c ^^^ a < b ∨ a ^^^ b < c
theorem Nat.lt_xor_cases E1 #
∀ {a b c : ℕ}, a < b ^^^ c → a ^^^ c < b ∨ a ^^^ b < c
theorem Nat.xor_mod_two_eq E0 #
∀ {m n : ℕ}, (m ^^^ n) % 2 = (m + n) % 2
theorem Nat.even_xor E0 #
∀ {m n : ℕ}, Even (m ^^^ n) ↔ (Even m ↔ Even n)
theorem Nat.xor_one_of_even E0 #
∀ {n : ℕ}, Even n → n ^^^ 1 = n + 1
theorem Nat.xor_one_of_odd E0 #
∀ {n : ℕ}, Odd n → n ^^^ 1 = n - 1
theorem Nat.xor_range E1 #
∀ (n : ℕ),
List.foldl (fun x1 x2 ↦ x1 ^^^ x2) 0 (List.range (n + 1)) =
match Fin.ofNat 4 n with
| 0 => n
| 1 => 1
| 2 => n + 1
| 3 => 0
The xor of the numbers from 0 to n can be easily calculated using n mod 4.
theorem Nat.shiftLeft_lt E1 #
∀ {x n m : ℕ}, x < 2 ^ n → x <<< m < 2 ^ (n + m)
theorem Nat.append_lt E1 #
∀ {x y n m : ℕ}, x < 2 ^ n → y < 2 ^ m → y <<< n ||| x < 2 ^ (n + m)
Note that the LHS is the expression used within Std.BitVec.append, hence the name.