I have to convert a given 16 bit integer into two 8 bit integers, which are then taken and used as output, where they are headed takes the two 8 bit integers and recombines them as 16 bit input (unfortunately out of my control). My solution works, but feels unclean. For the coarse number I am bit shifting the original number, and for the fine number I am looking at it modulo 256.
So should I be doing floor division for the coarse number, or should I be taking the lowest 8 bits for the fine number (and if so how?)?
Or am I crazy and using two different methods to split the number is not a problem?
def convert(x):
''' convert 16 bit int x into two 8 bit ints, coarse and fine.
'''
c = x >> 8 # The value of x shifted 8 bits to the right, creating coarse.
f = x % 256 # The remainder of x / 256, creating fine.
return c, f