【问题标题】:Python27 binary MSB LSB manipulatePython27二进制MSB LSB操作
【发布时间】:2020-06-02 17:30:57
【问题描述】:

我有一个我无法破解的简单问题。 我在 Python2.7 中使用 SMBus 从提供 med MSB 和 LSB 的芯片读取数据。

我需要删除 MSB 中的第 2 位,然后将其与 LSB 的最后 8 位组合。

喜欢:

MSB = bus.read_byte_data(address, register_1)
LSB = bus.read_byte_data(address, register_2)

MSB = 11110000,LSB = 11111111

MSB_cut = 110000 , LSB = 11111111

合并 = 11000011111111

然后将其转换为整数。

我已经尝试过按位运算,但我没有做对。我知道这很简单,所以有人可以向我展示正确的代码吗?

谢谢。

【问题讨论】:

    标签: python-2.7 binary bit-manipulation smbus


    【解决方案1】:

    好的

    我已经创建了这个非常丑陋的解决方案:

    MSB = bus.read_byte_data(adress, register_1)
    LSB = bus.read_byte_data(adress, register_2)
    mb = bin(MSB)
    lb = bin(LSB)
    tot = mb[4:] + lb[2:]
    total = int(tot, 2)
    

    我认为必须有一个更微妙的方法来解决这个问题。 :-)

    【讨论】:

      猜你喜欢
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      • 2010-10-19
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多