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
You should be consistent, if the intent of the operations is arithmetical, use modulo and division, if it's just for raw bit manipulation, use shift and mask.
I would use the bitwise & rather than %. It probably makes little difference these days for short integers, but in the wider view, the & operator is potentially more efficient.
There may be some issue about how % handles negative numbers, but I doubt that that's relevant here.
If you use the two halves of the number in different places, I'd recommend having two separate functions, but if you're going to use them in the same place, one function will work just as well.
There are several correct ways to split the number, so eventually it all comes down to personal preference. Your code will work fine, just so long as you only pass in numbers that are at most 16 bits long. (which probably won't be much of a problem, but you should be aware of it)
You say you're using these numbers as output, which suggests that they're going to be converted into strings at some point down the line. With that in mind, I'd suggest you take a look at the
struct
module, which is designed for precisely this sort of thing (packing numbers into strings of binary data). As a bonus, you get built-in error checking for the case wherex
is greater than 65535 (so that if something is horribly wonky in your program, you'll get an exception). For example,is the equivalent of
If you need the other byte ordering, you can write
If you have a whole bunch of numbers to convert at once,
struct.pack
can do them in bunches:I would do
It is safer, see e.g.
Since in python you can't control if the number is or not a 16bit, you have to force it into an at most 16-bit value. This is not needed if you're sure to have a 16-bit value, but this way the function is more general and allows you to be interested only in 16-bit values, no matter what the container contains.
In python, bit-fiddling doesn't have any particular advantage, so I would go with:
EDIT: To make your intention even more obvious to the powers-of-two-challenged source viewer (if such a beast exists), you can replace the plain
256
with much more colourful alternatives, like1<<8
,2**8
,0x100
or0400
. The constant folding done by the peephole optimizer since 2.5 ensures that any of them is exactly the same like using256
(I'm obviously talking about the former two alternatives, which are expressions that evaluate to256
; the latter two are the constant256
).