I’m trying to XOR binary values but for some unknown reason I’m losing the first 0 in the conversion. Any idea of what I’m doing wrong?
m1 = int('01010100011001010111001101110100',2)
c1 = int('00111001000001000001000000000001',2)
c2 = int('00111101000100110000110000010001',2)
kk = bin(m1 ^ c1)[2:]
xx = int(kk,2)
m2 = bin(xx ^ c2)[2:]
print m2
For some reason this returns 1010000011100100110111101100100 instead of 01010000011100100110111101100100, so I am losing the leading 0.
Python doesn’t know how many bits you want in the answer. Tell it:
The format string indicates a right-aligned, 32-character field padded with zeroes.
See the Format Specification Mini-Language.
In fact, you can just do the following with the
bformat specifier:or alternatively: