阅读Python中至少显著位 [英] Reading least significant bits in Python
问题描述
我不必解析Python中的syslog信息的工具和严重性。这些值来每条消息作为一个整数。该事件的严重性是0-7,在整数3至少显著位指定。什么是从数量评估这3位最简单的/最快的方式?
在code我现在只是做了3位右移,比乘这个数字8次,并从原来的中减去的结果。
FAC =(INT(PRI)GT;> 3)
SEV = PRI - (FAC * 8)
有必须要做这个 - 而不是歼灭位,减去一个令人费解的较小的方式。
(我的职业是系统管理员的话,我不知道有多少的basics-的请与我裸露!)
SEV = PRI和放大器; 7
FAC = PRI>> 3
这样的。
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 substracting.
(I am a sys admin by trade so, I don't know many of the basics- please bare with me!)
SEV = PRI & 7
FAC = PRI >> 3
Like that.
这篇关于阅读Python中至少显著位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!