The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V02. Numbers

Mathlib.Data.Num.Bitwise

103 declarations. Mathlib docs · Source

Bitwise operations using binary representation of integers

Definitions

  • bitwise operations for PosNum and Num,
  • SNum, a type that represents integers as a bit string with a sign bit at the end,
  • arithmetic operations for SNum.

The SNum representation uses a bit string, essentially a list of 0 (false) and 1 (true) bits, and the negation of the MSB is sign-extended to all higher bits.

abbrev NzsNum.brecOn.go E1 #

{motive : NzsNum → Sort u} → (t : NzsNum) → ((t : NzsNum) → NzsNum.below t → motive t) → motive t ×' NzsNum.below t

theorem SNum.nz.injEq E0 #

∀ (a a_1 : NzsNum), (SNum.nz a = SNum.nz a_1) = (a = a_1)

theorem NzsNum.brecOn.eq E1 #

∀ {motive : NzsNum → Sort u} (t : NzsNum) (F_1 : (t : NzsNum) → NzsNum.below t → motive t),
  NzsNum.brecOn t F_1 = F_1 t (NzsNum.brecOn.go t F_1).2

theorem NzsNum.bit.injEq E0 #

∀ (a : Bool) (a_1 : NzsNum) (a_2 : Bool) (a_3 : NzsNum), (NzsNum.bit a a_1 = NzsNum.bit a_2 a_3) = (a = a_2 ∧ a_1 = a_3)

theorem NzsNum.msb.injEq E0 #

∀ (a a_1 : Bool), (NzsNum.msb a = NzsNum.msb a_1) = (a = a_1)

abbrev NzsNum.ctorElimType E1 #

{motive : NzsNum → Sort u} → ℕ → Sort (max 1 u)

theorem NzsNum.msb.inj E1 #

∀ {a a_1 : Bool}, NzsNum.msb a = NzsNum.msb a_1 → a = a_1

abbrev SNum.ctorElimType E1 #

{motive : SNum → Sort u} → ℕ → Sort (max 1 u)

theorem NzsNum.bit.inj E1 #

∀ {a : Bool} {a_1 : NzsNum} {a_2 : Bool} {a_3 : NzsNum}, NzsNum.bit a a_1 = NzsNum.bit a_2 a_3 → a = a_2 ∧ a_1 = a_3

theorem SNum.zero.inj E1 #

∀ {a a_1 : Bool}, SNum.zero a = SNum.zero a_1 → a = a_1

def SNum.ctorIdx E1 #

SNum → ℕ

theorem SNum.nz.inj E1 #

∀ {a a_1 : NzsNum}, SNum.nz a = SNum.nz a_1 → a = a_1

theorem SNum.zero.injEq E0 #

∀ (a a_1 : Bool), (SNum.zero a = SNum.zero a_1) = (a = a_1)

def NzsNum.ctorIdx E1 #

NzsNum → ℕ

def PosNum.lor E1 #

PosNum → PosNum → PosNum

Bitwise "or" for PosNum.

instance PosNum.instOrOp E0 #

OrOp PosNum

theorem PosNum.lor_eq_or E0 #

∀ (p q : PosNum), p.lor q = p ||| q

def PosNum.land E1 #

PosNum → PosNum → Num

Bitwise "and" for PosNum.

instance PosNum.instHAndNum E0 #

HAnd PosNum PosNum Num

theorem PosNum.land_eq_and E0 #

∀ (p q : PosNum), p.land q = p &&& q

def PosNum.ldiff E1 #

PosNum → PosNum → Num

Bitwise fun a b ↦ a && !b for PosNum. For example, ldiff 5 9 = 4:

 101
1001
----
 100

def PosNum.lxor E1 #

PosNum → PosNum → Num

Bitwise "xor" for PosNum.

instance PosNum.instHXorNum E0 #

HXor PosNum PosNum Num

theorem PosNum.lxor_eq_xor E0 #

∀ (p q : PosNum), p.lxor q = p ^^^ q

def PosNum.testBit E1 #

PosNum → ℕ → Bool

a.testBit n is true iff the n-th bit (starting from the LSB) in the binary representation of a is active. If the size of a is less than n, this evaluates to false.

def PosNum.oneBits E1 #

PosNum → ℕ → List ℕ

n.oneBits 0 is the list of indices of active bits in the binary representation of n.

def PosNum.shiftl E1 #

PosNum → ℕ → PosNum

Left-shift the binary representation of a PosNum.

instance PosNum.instHShiftLeftNat E0 #

HShiftLeft PosNum ℕ PosNum

theorem PosNum.shiftl_eq_shiftLeft E0 #

∀ (p : PosNum) (n : ℕ), p.shiftl n = p <<< n

theorem PosNum.shiftl_succ_eq_bit0_shiftl E1 #

∀ (p : PosNum) (n : ℕ), p <<< n.succ = (p <<< n).bit0

def PosNum.shiftr E1 #

PosNum → ℕ → Num

Right-shift the binary representation of a PosNum.

instance PosNum.instHShiftRightNatNum E0 #

HShiftRight PosNum ℕ Num

theorem PosNum.shiftr_eq_shiftRight E0 #

∀ (p : PosNum) (n : ℕ), p.shiftr n = p >>> n

def Num.lor E1 #

Num → Num → Num

Bitwise "or" for Num.

instance Num.instOrOp E0 #

OrOp Num

theorem Num.lor_eq_or E0 #

∀ (p q : Num), p.lor q = p ||| q

def Num.land E1 #

Num → Num → Num

Bitwise "and" for Num.

instance Num.instAndOp E0 #

AndOp Num

theorem Num.land_eq_and E0 #

∀ (p q : Num), p.land q = p &&& q

def Num.ldiff E1 #

Num → Num → Num

Bitwise fun a b ↦ a && !b for Num. For example, ldiff 5 9 = 4:

 101
1001
----
 100

def Num.lxor E1 #

Num → Num → Num

Bitwise "xor" for Num.

instance Num.instXorOp E0 #

XorOp Num

theorem Num.lxor_eq_xor E0 #

∀ (p q : Num), p.lxor q = p ^^^ q

def Num.shiftl E1 #

Num → ℕ → Num

Left-shift the binary representation of a Num.

instance Num.instHShiftLeftNat E0 #

HShiftLeft Num ℕ Num

theorem Num.shiftl_eq_shiftLeft E0 #

∀ (p : Num) (n : ℕ), p.shiftl n = p <<< n

def Num.shiftr E1 #

Num → ℕ → Num

Right-shift the binary representation of a Num.

instance Num.instHShiftRightNat E0 #

HShiftRight Num ℕ Num

theorem Num.shiftr_eq_shiftRight E0 #

∀ (p : Num) (n : ℕ), p.shiftr n = p >>> n

def Num.testBit E1 #

Num → ℕ → Bool

a.testBit n is true iff the n-th bit (starting from the LSB) in the binary representation of a is active. If the size of a is less than n, this evaluates to false.

def Num.oneBits E1 #

Num → List ℕ

n.oneBits is the list of indices of active bits in the binary representation of n.

inductive NzsNum E1 #

Type

This is a nonzero (and "non minus one") version of SNum. See the documentation of SNum for more details.

ctor NzsNum.msb E0 #

Bool → NzsNum

ctor NzsNum.bit E0 #

Bool → NzsNum → NzsNum

Add a bit at the end of a NzsNum.

def instDecidableEqNzsNum.decEq E1 #

(x x_1 : NzsNum) → Decidable (x = x_1)

instance instDecidableEqNzsNum E0 #

DecidableEq NzsNum

inductive SNum E1 #

Type

Alternative representation of integers using a sign bit at the end. The convention on sign here is to have the argument to msb denote the sign of the MSB itself, with all higher bits set to the negation of this sign. The result is interpreted in two's complement.

13  = ..0001101(base 2) = nz (bit1 (bit0 (bit1 (msb true))))
-13 = ..1110011(base 2) = nz (bit1 (bit1 (bit0 (msb false))))

As with Num, a special case must be added for zero, which has no msb, but by two's complement symmetry there is a second special case for -1. Here the Bool field indicates the sign of the number.

0  = ..0000000(base 2) = zero false
-1 = ..1111111(base 2) = zero true

ctor SNum.zero E0 #

Bool → SNum

ctor SNum.nz E0 #

NzsNum → SNum

def instDecidableEqSNum.decEq E1 #

(x x_1 : SNum) → Decidable (x = x_1)

instance instDecidableEqSNum E0 #

DecidableEq SNum

instance instCoeNzsNumSNum E0 #

Coe NzsNum SNum

instance instZeroSNum E0 #

Zero SNum

instance instOneNzsNum E0 #

One NzsNum

instance instOneSNum E0 #

One SNum

instance instInhabitedNzsNum E0 #

Inhabited NzsNum

instance instInhabitedSNum E0 #

Inhabited SNum

def NzsNum.«term_::_» E1 #

Lean.TrailingParserDescr

Add a bit at the end of a NzsNum.

def NzsNum.sign E1 #

NzsNum → Bool

Sign of a NzsNum.

def NzsNum.not E1 #

NzsNum → NzsNum

Bitwise not for NzsNum.

def NzsNum.«term~_» E1 #

Lean.ParserDescr

Bitwise not for NzsNum.

def NzsNum.bit0 E1 #

NzsNum → NzsNum

Add an inactive bit at the end of a NzsNum. This mimics PosNum.bit0.

def NzsNum.bit1 E1 #

NzsNum → NzsNum

Add an active bit at the end of a NzsNum. This mimics PosNum.bit1.

def NzsNum.head E1 #

NzsNum → Bool

The head of a NzsNum is the Boolean value of its LSB.

def NzsNum.tail E1 #

NzsNum → SNum

The tail of a NzsNum is the SNum obtained by removing the LSB. Edge cases: tail 1 = 0 and tail (-2) = -1.

def SNum.sign E1 #

SNum → Bool

Sign of a SNum.

def SNum.not E1 #

SNum → SNum

Bitwise not for SNum.

def SNum.«term~_» E1 #

Lean.ParserDescr

Bitwise not for SNum.

def SNum.bit E1 #

Bool → SNum → SNum

Add a bit at the end of a SNum. This mimics NzsNum.bit.

def SNum.«term_::_» E1 #

Lean.TrailingParserDescr

Add a bit at the end of a SNum. This mimics NzsNum.bit.

def SNum.bit0 E1 #

SNum → SNum

Add an inactive bit at the end of a SNum. This mimics ZNum.bit0.

def SNum.bit1 E1 #

SNum → SNum

Add an active bit at the end of a SNum. This mimics ZNum.bit1.

theorem SNum.bit_zero E1 #

∀ (b : Bool), SNum.bit b (SNum.zero b) = SNum.zero b

theorem SNum.bit_one E1 #

∀ (b : Bool), SNum.bit b (SNum.zero (decide ¬b = true)) = SNum.nz (NzsNum.msb b)

def NzsNum.drec' E1 #

{C : SNum → Sort u_1} →
  ((b : Bool) → C (SNum.zero b)) → ((b : Bool) → (p : SNum) → C p → C (SNum.bit b p)) → (p : NzsNum) → C (SNum.nz p)

A dependent induction principle for NzsNum, with base cases 0 : SNum and (-1) : SNum.

def SNum.head E1 #

SNum → Bool

The head of a SNum is the Boolean value of its LSB.

def SNum.tail E1 #

SNum → SNum

The tail of a SNum is obtained by removing the LSB. Edge cases: tail 1 = 0, tail (-2) = -1, tail 0 = 0 and tail (-1) = -1.

def SNum.drec' E1 #

{C : SNum → Sort u_1} →
  ((b : Bool) → C (SNum.zero b)) → ((b : Bool) → (p : SNum) → C p → C (SNum.bit b p)) → (p : SNum) → C p

A dependent induction principle for SNum which avoids relying on NzsNum.

def SNum.rec' E1 #

{α : Sort u_1} → (Bool → α) → (Bool → SNum → α → α) → SNum → α

An induction principle for SNum which avoids relying on NzsNum.

def SNum.testBit E1 #

ℕ → SNum → Bool

SNum.testBit n a is true iff the n-th bit (starting from the LSB) of a is active. If the size of a is less than n, this evaluates to false.

def SNum.succ E1 #

SNum → SNum

The successor of a SNum (i.e. the operation adding one).

def SNum.pred E1 #

SNum → SNum

The predecessor of a SNum (i.e. the operation of removing one).

def SNum.neg E1 #

SNum → SNum

The opposite of a SNum.

instance SNum.instNeg E0 #

Neg SNum

def SNum.czAdd E1 #

Bool → Bool → SNum → SNum

SNum.czAdd a b n is n + a - b (where a and b should be read as either 0 or 1). This is useful to implement the carry system in cAdd.

def SNum.bits E1 #

SNum → (n : ℕ) → List.Vector Bool n

a.bits n is the vector of the n first bits of a (starting from the LSB).

def SNum.cAdd E1 #

SNum → SNum → Bool → SNum

SNum.cAdd n m a is n + m + a (where a should be read as either 0 or 1). a represents a carry bit.

def SNum.add E1 #

SNum → SNum → SNum

Add two SNums.

instance SNum.instAdd E0 #

Add SNum

def SNum.sub E1 #

SNum → SNum → SNum

Subtract two SNums.

instance SNum.instSub E0 #

Sub SNum

def SNum.mul E1 #

SNum → SNum → SNum

Multiply two SNums.

instance SNum.instMul E0 #

Mul SNum