binary - Python: How to convert array of 4 digits to a larger number, bitwise? -
let's have
array = [1,2,3,4]
what want not convert number 1234; take bits of 1, 2, 3 , 4, concatenate them , convert number.
in other words, have perhaps convert each number/digit binary, concatenate them , convert number.
therefore, 1,2,3,4 00000001
, 00000010
, 00000011
, 00000100
respectively. concatenating them lead 00000001000000100000001100000100
converted unsigned int 16909060
keep in mind digits array come ord(characters)
, should 8bits in length, therefore concatenated should lead 32bit number
how that?
in simple case perhaps suffices:
result = array[0] << 24 | array[1] << 16 | array[2] << 8 | array[3]
for example:
array = [1, 2, 3, 4] result = array[0] << 24 | array[1] << 16 | array[2] << 8 | array[3] print result
prints this:
16909060
Comments
Post a Comment