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
@[instance]
Equations
- snum.has_coe = {coe := snum.nz}
@[instance]
@[instance]
Equations
@[instance]
Equations
- nzsnum.inhabited = {default := 1}
def
nzsnum.drec'
{C : snum → Sort u_1}
(z : Π (b : bool), C (snum.zero b))
(s : Π (b : bool) (p : snum), C p → C (b :: p))
(p : nzsnum) :
C ↑p
Equations
- nzsnum.drec' z s (b :: p) = s b ↑p (nzsnum.drec' z s p)
- nzsnum.drec' z s (nzsnum.msb b) = _.mpr (s b (snum.zero (!b)) (z (!b)))
def
snum.drec'
{C : snum → Sort u_1}
(z : Π (b : bool), C (snum.zero b))
(s : Π (b : bool) (p : snum), C p → C (b :: p))
(p : snum) :
C p
Equations
- snum.drec' z s (snum.nz p) = nzsnum.drec' z s p
- snum.drec' z s (snum.zero b) = z b
Equations
- snum.rec' z s = snum.drec' z s
Equations
- snum.test_bit (n + 1) p = snum.test_bit n p.tail
- snum.test_bit 0 p = p.head
Equations
- snum.czadd bool.tt bool.tt p = p
- snum.czadd bool.tt bool.ff p = p.succ
- snum.czadd bool.ff bool.tt p = p.pred
- snum.czadd bool.ff bool.ff p = p