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, specified in the 3 least significant bits in the integer. What is the easiest/fastest way to evaluate these 3 bits from the number?
The code I have right now just does a 3 bit right shift, than multiplies that number times 8, and subtracts the result from the original.
FAC = (int(PRI) >> 3)
SEV = PRI - (FAC * 8)
There must be a less convoluted way to do this- rather than wiping out the bits, and subtracting.
SEV = PRI & 7
FAC = PRI >> 3
Like that.
Just apply a bit mask:
sev = int(pri) & 0x07
(0x07 is 00000111)
请尝试以下方法
result = FAC & 0x7
提取最低有效位的常规方法是使用适当的掩码进行按位AND(在本例中为7)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.