Reading least significant bits in Python

前端 未结 4 1242
忘掉有多难
忘掉有多难 2021-01-20 07:59

I am having to parse the Facility and Severity of syslog messages in Python. These values come with each message as a single integer. The severity of the event is 0-7, spe

相关标签:
4条回答
  • 2021-01-20 08:04

    Try the following

    result = FAC & 0x7
    
    0 讨论(0)
  • 2021-01-20 08:11

    The normal way to extract the least significant bits would be to do a bitwise AND with the appropriate mask (7 in this case)

    0 讨论(0)
  • 2021-01-20 08:20
    SEV = PRI & 7
    FAC = PRI >> 3
    

    Like that.

    0 讨论(0)
  • 2021-01-20 08:20

    Just apply a bit mask:

    sev = int(pri) & 0x07
    

    (0x07 is 00000111)

    0 讨论(0)
提交回复
热议问题