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 #
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 positioni
that contains the most significant1
-bit ofn
.lt_of_testBit
: ifn
andm
are numbers andi
is a position such that thei
-th bit of ofn
is zero, thei
-th bit ofm
is one, and all more significant bits are equal, thenn < m
.
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
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 < j → Nat.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 < j → Nat.testBit n j = Nat.testBit m j)
:
n < m
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
- Nat.tacticBitwise_assoc_tac = Lean.ParserDescr.node `Nat.tacticBitwise_assoc_tac 1024 (Lean.ParserDescr.nonReservedSymbol "bitwise_assoc_tac" false)