Working with binary in Python, Splitting numbers -


i have module takes 2 x 8bit numbers in decimal format, specific structure

each number must start same 4 bits = 0011 followed varible 8 bits followed 4 bits ignored set 0000

so caculate 16bit number simple enough

the varible number * 16 shift 4 bits left, , adding 12288 = 0011|000000000000 give me desired result.

so if input number 19 example

19 x 16 + 12288 = 12592 = 0011000100110000

the next step split in 2 x 8 bit numbers

00110001 | 00110000 = 49, 48

how in python can go 12592 49,48 efficiently.

never worked in binary in script bit new.

cheers

to first 8 bits, shift right 8 bits.

   0011000100110000 >> 8 ==         00110001 

to last 8 bits, mask 0b11111111, i.e. 255.

   0011000100110000 &  0000000011111111 -------------------    0000000000110000 

code example:

in [1]: n = int("0011000100110000", 2) in [2]: n out[2]: 12592 in [8]: n >> 8, n & 255 out[8]: (49, 48) 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -