您如何找到字节中的位值 [英] How do you find value of bits in byte

查看:145
本文介绍了您如何找到字节中的位值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了所有的书,在网上也找不到办法,它只是告诉我一些不合适的移位位.

我有一个程序可以接收一个字节[]来自外部来源.
数组元素表示不同的内容,例如(byte [0]位1(最右边))=蜂鸣器状态(1开:0关)

在命令中我在那里接收到将是3个字节,每个包含的位代表一个蜂鸣器或LED,并且其状态为开或关;

有人知道我如何查看这些位并检查那里的状态吗?

感谢George

I have looked in all my books and on the net and cant find a way to do this, it just tells me about shifting bits which will not be appropriate.

I have a program that recieves a byte[] from an external source.
The array elements represent different things eg(byte[0] bit 1 (rightmost)) = buzzer state (1 on: 0 off)

In the command I recieve there will be 3 bytes, with each contained bit representing a buzzer or LED and its state on or off;

Does anyone know how I can look through the bits and check there state?

Thanx George

推荐答案

使用AND和其他布尔值(如果要读取多个位)是一个问题.基本上,您使用以下公式:

bitNSet =(originalInteger&(1< N)== 1<< N)




二进制序列.例如,39用100111表示.每个1和0是一个比特.要获得一个位,您必须对其进行与"运算,这将获得在给定的两个数字中设置的所有位.例如,39 AND 4的计算方式如下:

100111 AND
000100 =
------ <000> 100100

如您所见,您最终得到一个二进制序列,该序列由十进制的4(以10为基数)表示.您对位模式使用AND,以查看该位是否已设置.现在,不幸的是,您没有得到正确或错误的值.请注意,39 AND 4的结果为4.因此,我们可以进一步应用它,并说如果q AND n等于n,则设置了n位.对AND使用的二进制模式.为此,我们只需使用<<操作员.它将在左操作数上执行左二进制移位.所以1<< 5将导致100000,1 << 2将得出100,以此类推.顺便说一下,这些数字都是二进制格式.因此,将N左移1将为我们提供仅设置第N位的二进制模式.通过与原始整数进行与"运算,我们可以检查该位是否已设置

It''s a matter of using ANDs, and other boolean stuff (if you want to read multiple bits). Basically, you use the formula:

bitNSet = (originalInteger & (1 << N) == 1 << N)

Effectively, every integer is represented by a binary sequence. For example, 39 would be represented by 100111. Each one and zero is a bit. To get a bit, you have to AND it, which gets all the bits which are set in both of the numbers given to it. For example, 39 AND 4 would be worked out like so:

100111 AND
000100 =
------
000100

As you can see, you end up with a binary sequence, which is represented by 4 in decimal (base-10). You use the AND against a bit pattern to see if that bit is set. Now, unfortunately, you don''t get a true or false value. Note that the result of 39 AND 4 was 4. So, we can apply this further, and say that if q AND n is equal to n, then bit n was set.

Now we just have to get a binary pattern to use against the AND. To get this, we just use the << operator. It will perform a left binary shift on the left operand. So 1 << 5 would result in 100000, 1 << 2 would result in 100, etc. Those numbers are in binary format by the way. So, shifting 1 left by N will give us a binary pattern where only the Nth bit is set. By ANDing that with the original integer, we can check if the bit is set


除了给出的出色建议外,所有布尔逻辑运算符变体易于实现,如下所示.另外,将枚举与Flags属性一起使用可以使获取单个位更容易,因为可以为枚举成员赋予更多描述性名称.
In addition to the excellent advice you''ve been given - all the Boolean logical operator variants are easy to implement as shown below. Also, using an enum with the Flags attribute can make getting individual bits easier as you can give your enum members more descriptive names.
class Program
{
    static void Main(string[] args)
    {
        byte baseValue = 0x01;
        byte otherValue = 0x07;
        Console.WriteLine("Base Value = {0} : Other Value = {1}", baseValue, otherValue);
        Console.WriteLine("AND result = {0}", baseValue & otherValue);
        Console.WriteLine("OR result = {0}", baseValue | otherValue);
        Console.WriteLine("XOR result = {0}", baseValue ^ otherValue);
        Console.WriteLine("NOT result (Base only) = {0}", (byte)~baseValue);
        Console.WriteLine("NAND result = {0}", (byte)~(baseValue & otherValue));
        Console.WriteLine("NOR result = {0}", (byte)~(baseValue | otherValue));

        Console.WriteLine("Bit0 of Base Value = {0}", baseValue & (byte)Bits.Bit0);
        Console.WriteLine("Bit0 and Bit1 of Other Value = {0}", otherValue & (byte)(Bits.Bit0 | Bits.Bit1));
        Console.ReadKey();
    }
}

[Flags]
public enum Bits : byte
{
    None = 0,
    Bit0 = 1,
    Bit1 = 2,
    Bit2 = 4,
    Bit3 = 8,
    Bit4 = 16,
    Bit5 = 32,
    Bit6 = 64,
    Bit7 = 128
}


如果要直接使用二进制值,那么不幸的是,C#中没有二进制文字.我写了这篇文章 [


If you want to work with binary values directly, unfortunately there is no binary literal in C#. I made this article[^] quite a while ago to make it easier to work with binary strings, you may find it of some use.


我认为您已经从以前的帖子中得到了答案.在第一篇文章中,使用了两个移位操作和一个比较.我认为在这种情况下不需要两个移位操作.您可以使用一个右移代替两个左移操作.请参考以下代码.

I think already you got the answer from the previous posts. In the first post, uses two shift operations and one comparison.I think there is no need of two shift operations in this case. You can use one right shift instead of two left shift operations.Refer the following code.

bool GetBit(byte thebyte, int position)
   {
     return (1 == ((thebyte >> position) & 1));
   }



在这种情况下,我们首先将给定位置的位移到最右边的位置.
例如:如果字节:0000 1001
位置:3
移位字节后:0000 000 1
现在,第3位(这里我使用从零开始的索引)成为最右边的位.
与1进行与"运算将得出该位的值.

谢谢,
Vineeth



In this case first we shifted the bit of the given position to the right most position.
eg : if byte : 0000 1001
position : 3
After shifting the byte : 0000 0001
Now the 3rd bit(Here i using zero-based index) become the rightmost bit.
ANDing with 1 will give the bit value.

Thanks,
Vineeth


这篇关于您如何找到字节中的位值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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