Discrete helpers/binv
binv |
This is a simple helper class for binary vectors.
from discretehelpers.binv import Binv
binv = Binv('1101000')
# properties
assert binv.length == 7
assert binv.weight == 3
assert binv.vector == [True, True, False, True, False, False, False]
assert binv.string == '1101000'
assert binv.exposet == {0, 1, 3} # little-endian
assert binv.intval == 11 # 2**0 + 2**1 + 2**3
assert binv.changes == 3 # changes between 0 and 1 (down, up, down)
assert binv.complement == ~binv == Binv('0010111')
# equivalent instantiations
assert binv == Binv('11 01 000')
assert binv == Binv([1, 1, 0, 1, 0, 0, 0])
assert binv == Binv(intval=11, length=7)
assert binv == Binv(exposet={0, 1, 3}, length=7)
The property pretty
can be used when the length is a power of two up to 64:
Binv(intval=38505).pretty == '1001 0110 0110 1001'
integer values
editAn important use is conversion to and from decimal. The interpretation is little-endian, i.e. the least-significant bit is on the left.
for i in range(16):
s = Binv(intval=i, length=4).string
padded_i = str(i).zfill(2)
print(padded_i, s)
00 0000 01 1000 02 0100 03 1100 04 0010 05 1010 06 0110 07 1110 08 0001 09 1001 10 0101 11 1101 12 0011 13 1011 14 0111 15 1111