位掩码-如何检测单个位 [英] Bitmask - How to detect single bits

查看:93
本文介绍了位掩码-如何检测单个位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试在一个字节中检测以下位:

前8位:全部设置为一个(&HFF)-相当容易.

但是:
9.-11位:也全部设置为一个
12.位:可以为0或1
13.位:可以为0或1
14-15位:0-0,0-1,1-0或1-1

有谁知道我该如何做到这一点(在C#或VB中)?

非常感谢!



Daniel

Hi,

I am trying to detect the following bits in one byte:

First 8 bit: all set to one (&HFF) - that''s quite easy.

But:
9. - 11 bit: also all set to one
12. bit: can be 0 or 1
13. bit: can be 0 or 1
14 - 15 bit: 0-0,0-1,1-0 or 1-1

Does anyone know how I can achieve this (in C# or VB)?

Thanks a lot!



Daniel

推荐答案

9. - 11 bit: also all set to one
12. bit: can be 0 or 1
13. bit: can be 0 or 1
14 - 15 bit: 0-0,0-1,1-0 or 1-1


这意味着仅需要设置12个最右边的位.该值是
& 07ffh或2047.

您可以像这样检查这些位:

VB.NET


This means that only the 12 most right bits need to be set. The value for that is
&07ffh, or 2047.

You can check those bits like this:

VB.NET

if ((value And 2047) = 2047) then



C#



C#

if ((value & 2047) == 2047) 



因为只想检查12个最正确的位,所以按位执行并丢弃所有其他位,因为它们无关紧要.然后,检查该值是否相等,以确保已设置所有需要检查的位.

祝你好运!



Because you want to check only the 12 most right bits, a bitwise and is performed to discard all other bits because they don''t matter. Then you check if the value is equal to make sure that all the bits you need to check are set.

Good luck!


我们在这里谈论多少位?首先,您指的是检测字节中的位,然后开始讨论第9至12位.一个字节中恰好有8位.

对于问题,将值读入字节数组,第一个字节为0xff.如果结果等于0xff,则下一个字节为0x07.如果等于0x07,那么您就完成了.您正在寻找的模式似乎是xxxxx111 11111111,听起来好像是读取它的方式(或者至少是您测试它的方式)是LSB在先.如果不是这种情况,请颠倒测试顺序.

顺便说一句,位和字节传统上是从0到n进行编号,最低有效位是0.如果您习惯以这种方式思考它们,那么您将避免很多混乱,因为大多数技术文档都使用这种方式.这种格式.

另外, [
How many bits are we talking about here? First you refer to detecting the bits in a byte, then start talking about the 9th thru 12 bit. There are exactly 8 bits in a byte.

As for the problem, read the values into an array of bytes, AND the first byte with 0xff. If that turns out to be equal to 0xff, AND the next byte with 0x07. If that''s equal to 0x07, you''re done. The pattern you''re looking for appears to be xxxxx111 11111111, and it sounds as if the manner of reading it (or at least the way you''re testing it) is LSB first. If that''s not the case, reverse the order of the tests.

By the way, bits and bytes are traditionally numbered 0 - n, with the least significant being 0. You''ll save yourself a whole lot of confusion if you get into the habit of thinking of them that way, as most technical documentation uses this format.

Also, this[^] class comes in handy for these types of problems. Good luck! :-D


C#不是用于较低级别编程的好语言.

这是我将在C中执行的操作:

C# is not a good language for lower level programming.

Here is how I will do it in C:

if ((hex_number & 0x7FF) == 0x7FF)
{
  // bits detected
}
else
{
  // bits not detected
}


在C#中,如下所示:
注意:& ->按位和运算符,而不是布尔运算符!


In C# probebly something like this:
NOTE: & -> bitwise and operator, not a boolean operator!!

if ((hex_number & &H7FF) == &H7FF)
{
  // bits detected
}
else
{
  // bits not detected
}



如果要检查其最高有效位,请使用& H1FFC00000而不是& H7FF .

问候,
鲁道夫

P.S.请投票!!



If its the most significant bits that you want to check then use, &H1FFC00000 instead of &H7FF.

Regards,
Rudolf

P.S. Please Vote!!


这篇关于位掩码-如何检测单个位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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