阅读Python中至少显著位 [英] Reading least significant bits in Python

查看:154
本文介绍了阅读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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆