小编nrn*_*nrn的帖子

在python中将16位int拆分为两个8位整数

我必须将给定的16位整数转换为两个8位整数,然后将其作为输出并用作输出,其中它们将获取两个8位整数并将它们重新组合为16位输入(不幸的是我无法控制).我的解决方案有效,但感觉不洁净.对于粗数我移位原始数字,对于细数,我看它模256.

那么我应该为粗数进行分区,还是应该将最低8位用于精确数字(如果是这样的话,怎么样?)?

或者我疯了,使用两种不同的方法来分割数字不是问题?

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
Run Code Online (Sandbox Code Playgroud)

python math

5
推荐指数
2
解决办法
1万
查看次数

标签 统计

math ×1

python ×1