Bit masking in Python

前端 未结 4 881
臣服心动
臣服心动 2021-02-14 04:11

I have a byte (from some other vendor) where the potential bit masks are as follows:

value1 = 0x01 value2 = 0x02 value3 = 0x03 value4 = 0x04 value5 = 0x05 value6 = 0x06

4条回答
  •  清歌不尽
    2021-02-14 04:58

    It's a little verbose but perfectly fine. The only change I'd make is to simplify parse_byte:

    def parse_byte(byte):
    
         value7_set = byte & value7 == value7
         value8_set = byte & value8 == value8
         base_value = mask_bits_on_byte(byte,value7 | value8)
         return value7_set,value8_set,base_value  
    

提交回复
热议问题