mathlib documentation

data.num.bitwise

Bitwise operations using binary representation of integers #

Definitions #

Bitwise "or" for pos_num.

Equations
def pos_num.land  :

Bitwise "and" for pos_num.

Equations
def pos_num.ldiff  :

Bitwise λ a b, a && !b for pos_num. For example, ldiff 5 9 = 4:

101

1001 #

100
Equations
def pos_num.lxor  :

Bitwise "xor" for pos_num.

Equations
def pos_num.test_bit  :
pos_numbool

a.test_bit n is tt 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 ff.

Equations
def pos_num.one_bits  :

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

Equations
def pos_num.shiftl (p : pos_num) :

Left-shift the binary representation of a pos_num.

Equations
def pos_num.shiftr  :
pos_numnum

Right-shift the binary representation of a pos_num.

Equations
def num.lor  :
numnumnum

Bitwise "or" for num.

Equations
def num.land  :
numnumnum

Bitwise "and" for num.

Equations
def num.ldiff  :
numnumnum

Bitwise λ a b, a && !b for num. For example, ldiff 5 9 = 4:

101

1001 #

100
Equations
def num.lxor  :
numnumnum

Bitwise "xor" for num.

Equations
def num.shiftl  :
numnum

Left-shift the binary representation of a num.

Equations
def num.shiftr  :
numnum

Right-shift the binary representation of a pos_num.

Equations
def num.test_bit  :
numbool

a.test_bit n is tt 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 ff.

Equations
def num.one_bits  :

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

Equations
@[protected, instance]
@[protected, instance]
inductive nzsnum  :
Type

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

Instances for nzsnum
@[protected, instance]
@[protected, instance]
inductive snum  :
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 tt))))
-13 = ..1110011(base 2) = nz (bit1 (bit1 (bit0 (msb ff))))

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 ff
-1 = ..1111111(base 2) = zero tt
Instances for snum
@[protected, instance]
Equations
@[protected, instance]
Equations
@[protected, instance]
Equations
@[protected, instance]
Equations
@[protected, instance]
Equations
@[protected, instance]
Equations

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

def nzsnum.sign  :

Sign of a nzsnum.

Equations
def nzsnum.not  :

Bitwise not for nzsnum.

Equations
def nzsnum.bit0  :

Add an inactive bit at the end of a nzsnum. This mimics pos_num.bit0.

Equations
def nzsnum.bit1  :

Add an active bit at the end of a nzsnum. This mimics pos_num.bit1.

Equations
def nzsnum.head  :

The head of a nzsnum is the boolean value of its LSB.

Equations
def nzsnum.tail  :

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

Equations
def snum.sign  :

Sign of a snum.

Equations
def snum.not  :

Bitwise not for snum.

Equations
def snum.bit  :
boolsnumsnum

Add a bit at the end of a snum. This mimics nzsnum.bit.

Equations
def snum.bit0  :

Add an inactive bit at the end of a snum. This mimics znum.bit0.

Equations
def snum.bit1  :

Add an active bit at the end of a snum. This mimics znum.bit1.

Equations
theorem snum.bit_one (b : bool) :
def nzsnum.drec' {C : snumSort u_1} (z : Π (b : bool), C (snum.zero b)) (s : Π (b : bool) (p : snum), C pC (b::p)) (p : nzsnum) :
C p

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

Equations
def snum.head  :

The head of a snum is the boolean value of its LSB.

Equations
def snum.tail  :

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.

Equations
def snum.drec' {C : snumSort u_1} (z : Π (b : bool), C (snum.zero b)) (s : Π (b : bool) (p : snum), C pC (b::p)) (p : snum) :
C p

A dependent induction principle for snum which avoids relying on nzsnum.

Equations
def snum.rec' {α : Sort u_1} (z : bool → α) (s : boolsnumα → α) :
snum → α

An induction principle for snum which avoids relying on nzsnum.

Equations
def snum.test_bit  :
snumbool

snum.test_bit n a is tt 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 ff.

Equations
def snum.succ  :

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

Equations
def snum.pred  :

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

Equations
@[protected]
def snum.neg (n : snum) :

The opposite of a snum.

Equations
@[protected, instance]
Equations
def snum.czadd  :
boolboolsnumsnum

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.

Equations
def snum.bits  :
snumΠ (n : ), vector bool n

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

Equations
def snum.cadd  :
snumsnumboolsnum
Equations
@[protected]
def snum.add (a b : snum) :

Add two snums.

Equations
@[protected, instance]
Equations
@[protected]
def snum.sub (a b : snum) :

Substract two snums.

Equations
@[protected, instance]
Equations
@[protected]
def snum.mul (a : snum) :

Multiply two snums.

Equations
@[protected, instance]
Equations