math - Python: how to sum binaries? From binary-power-vector presentation to real binary presentation, how? -
i trying express list express binary number real binary number (1,3,6) means 0b100101
. first, try sum them 2^1+2^3+2^6 , convert binary
with open('data.txt') f: line in f: myline=line.rstrip().split("\t") print [bin(2**int(l)) l in myline[1:5]]
where converted list a
below so
>>> a=['0b10000000000000000000000000', '0b100000000000', '0b100000000000000000000000000000'] >>> a[1]|a[2] traceback (most recent call last): file "<stdin>", line 1, in <module> typeerror: unsupported operand type(s) |: 'str' , 'str' >>> bin(a[1])+bin(a[2]) traceback (most recent call last): file "<stdin>", line 1, in <module> typeerror: 'str' object cannot interpreted index
so
how should convert power vector binary number i.e. how can power-binary number presentation real binary number presentation?
simple example
input
['0b10000000000000000000000000', '0b100000000000', '0b100000000000000000000000000000']
intended output
0b100010000000000000100000000000
bakuriu explained going on well, i’ll make short. solution problem is
with open('data.txt') f: line in f: myline=line.rstrip().split("\t") print sum(2**int(l) l in myline[1:5])
note summation equal binary or, long different bits set, assume case here.
Comments
Post a Comment