关于二进制和序列信息的问题 [英] question about binary and serial info

查看:68
本文介绍了关于二进制和序列信息的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开展一个有趣的项目。这是挑战。

我正在使用串行模块从串行输入中读取数据。

它以十六进制形式出现。我需要将它作为二进制文件并将其与另一个字节进行比较。他们有一些奇怪的方式,他们设置了这个我需要将这些东西与AND进行比较。换句话说,如果第1位是1

而第1位是1那么实际值是1 ......


长话短说。有没有一种很好的方法来比较字节,一点一点地用和/ $
python的一个模块。我想知道所以我没有进入中途

开发这个并发现有一个更好的方法来做到这一点

而不是手工。


感谢任何建议。

sk<><

解决方案

听起来你想要按位和运算符,&

2& 3
2 32& 16
0 31& 12


12




neph ... @ xit.net查询:我正在进行一个有趣的项目。这是挑战。
我正在使用串行模块从串行输入中读取数据。
它以十六进制形式出现。我需要使它成为一个二进制文件并将其与另一个字节进行比较。他们有一些奇怪的方式来设置它,我必须将这些东西与AND进行比较。换句话说,如果第1位是1
AND第1位是1那么实际值是1 ......

长话短说。是否有一种很好的方法来比较字节,一点一点地与python的一个模块进行比较。我想知道所以我不会半途而废地开发这个并发现有更好的方法来完成这个
而不是手工。

感谢您的任何建议。
sk<><




是的,我想我明白了,我需要帮助才能获得十六进制

二进制,然后将字节分开以将每个位与另一个字节中的位

进行比较。

除非我不理解这些东西按位右。那个

在python库参考中没有多少关于它。

感谢



ne*****@xit.net 写道:

是的,我我认为我得到了这个,我需要帮助将十六进制转换为二进制,然后将字节拆分为比较每个位与另一个字节中的位



&操作员同时进行所有8次比较。因此,如果串口字节为A,那么参考字节为B,那么


AB = A& B


只有1位,其中A和B在各自的

位置都有1'。现在,您可以测试AB的特定位位置

(比如第3位)


testbit3 = AB& 2 ** 3

如果testbit3> 0然后该位是1.

除非我不理解按位右边的这个东西。有关它的python库参考中没有很多东西。


GMPY模块有一些有趣的位功能。


Popcount可以告诉你有多少AB位是1而没有

指定哪些:

for i in range(16):
print gmpy .popcount(i),


0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4


汉明距离告诉你

两个数字之间的位数有多少(再次,没有告诉你哪些)
我在范围内的
(16):
print gmpy.hamdist(i ,7),


3 2 2 1 2 1 1 0 4 3 3 2 3 2 2 1


如果一个操作数为0,然后汉明距离是相同的

作为popcount。


然后有扫描1会告诉你这一点

第1位的位位置。

A = 48
B = 255
AB = A& B
打印gmpy.scan1(AB)



4


所以前1位是位4,这意味着位0,1,2和3

都是0.

谢谢




i have an interesting project at work going on. here is the challenge.
i am using the serial module to read data from a serial input.
it comes in as a hex. i need to make it a binary and compare it bit by
bit to another byte. They have some weird way they set this up that i
have to compare these things with AND. in other words, if bit 1 is 1
AND bit 1 is 1 then the real value is 1...

long story short. is there a good way to compare bytes, bit by bit with
one of the modules of python. i want to know so i dont get halfway into
developing this and find that there is a much better way to do this
than by hand.

thanks for any suggestions.
sk <><

解决方案

Sounds like you want the bitwise and operator, &

2 & 3 2 32 & 16 0 31 & 12

12

etc.

neph...@xit.net inquired: i have an interesting project at work going on. here is the challenge.
i am using the serial module to read data from a serial input.
it comes in as a hex. i need to make it a binary and compare it bit by
bit to another byte. They have some weird way they set this up that i
have to compare these things with AND. in other words, if bit 1 is 1
AND bit 1 is 1 then the real value is 1...

long story short. is there a good way to compare bytes, bit by bit with
one of the modules of python. i want to know so i dont get halfway into
developing this and find that there is a much better way to do this
than by hand.

thanks for any suggestions.
sk <><




yeah, i think i got that down, i need help with getting the hex to
binary, then splitting the byte up to compare each bit against the bit
in another byte.
unless i am not understanding this stuff with the bitwise right. there
wasn''t a lot in the python library reference about it.
thanks



ne*****@xit.net wrote:

yeah, i think i got that down, i need help with getting the hex to
binary, then splitting the byte up to compare each bit against the bit
in another byte.
The & operator does all 8 comparisons simultaneously. So if
the serial port byte is A, the reference byte is B then

AB = A & B

has only 1 bits where both A and B had 1''s in their respective
positions. Now, you can test AB for a particular bit position
(say bit 3) by

testbit3 = AB & 2**3

If testbit3 > 0 then the bit was a 1.
unless i am not understanding this stuff with the bitwise right. there
wasn''t a lot in the python library reference about it.
The GMPY module has some interesting bit functions.

Popcount can tell you how many of the AB bits are 1 without
specifying which ones:

for i in range(16): print gmpy.popcount(i),

0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4

Hamming distance tells you how many bits differ between
two numbers (again, without telling you which ones)
for i in range(16): print gmpy.hamdist(i,7),

3 2 2 1 2 1 1 0 4 3 3 2 3 2 2 1

If one operand is 0, then Hamming distance is the same
as popcount.

And then there''s scan1 which will tell you the bit
bit position of the first 1 bit.
A = 48
B = 255
AB = A & B
print gmpy.scan1(AB)


4

So the first 1 bit is bit 4, which means bits 0, 1, 2 and 3
are all 0.
thanks




这篇关于关于二进制和序列信息的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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