简体   繁体   English

C# 从字节中获取位

[英]C# Get Bits from Byte

I don't know why I am getting wrong bits when reading my byte.我不知道为什么在读取我的字节时我得到了错误的位。 I have followed some indications found here, bit it is not working.我已经按照这里找到的一些迹象进行操作,但它不起作用。 I show you my code:我给你看我的代码:

byte[] byte22And23 = _packet.ReadBytes(2);
DataFromGTAPackets.Packet8.ControlFlags.rightEngineSmoke = _packet.GetBit(byte22And23[0], 0);
DataFromGTAPackets.Packet8.ControlFlags.leftEngineSmoke = _packet.GetBit(byte22And23[0], 1);
DataFromGTAPackets.Packet8.ControlFlags.rightEngineFire = _packet.GetBit(byte22And23[0], 2);
DataFromGTAPackets.Packet8.ControlFlags.leftEngineFire = _packet.GetBit(byte22And23[0], 3);
DataFromGTAPackets.Packet8.ControlFlags.rightRotorFail = _packet.GetBit(byte22And23[0], 4);
DataFromGTAPackets.Packet8.ControlFlags.leftRotorFail = _packet.GetBit(byte22And23[0], 5);
etc...
public bool GetBit(byte b, int bitNumber)
    {
        bool bit = (b & (1 << bitNumber - 1)) != 0;
        return bit;
    }

My byte 22 has a value of 2, it is: 00000010. And my byte 23 has a value of 0. When I use this code to read byte 22, my 3rd variable (rightEngineFire) is getting "true" (1).我的字节 22 的值为 2,它是:00000010。我的字节 23 的值为 0。当我使用此代码读取字节 22 时,我的第三个变量(rightEngineFire)变得“真”(1)。 It has no sense, it is wrong obviously.毫无意义,显然是错误的。 But I don't know what is wrong.但我不知道出了什么问题。

Thank you!谢谢!

You GetBit method considers bit numbers to be 1...32, instead of being 0...31.GetBit方法将位数视为 1...32,而不是 0...31。

Simple test:简单测试:

bool b1 = GetBit(1, 0); // false
bool b2 = GetBit(1, 1); // true

You should change the method to您应该将方法更改为

public static bool GetBit(byte b, int bitNumber)
{
    bool bit = (b & (1 << bitNumber)) != 0;
    return bit;
}

Or you could write:或者你可以写:

DataFromGTAPackets.Packet8.ControlFlags.rightEngineSmoke = _packet.GetBit(byte22And23[0], 1);

But in C-derived languages (and C# is a C-derived language) we count from 0, so the first solution is better (for me).但是在 C 派生语言(并且 C# 是 C 派生语言)中,我们从 0 开始计数,所以第一个解决方案更好(对我来说)。

And as a sidenote:作为旁注:

1 << bitNumber - 1

is quite unreadable, because probably 9 out of 10 programmers don't know/don't remember that the expression means很不可读,因为可能有十分之九的程序员不知道/不记得表达式的意思

1 << (bitNumber - 1)

because in the operator precedence table the - comes before the << , so when you mix operators you should use (...) to make clear the priority.因为在运算符优先级表-<<之前,所以当你混合运算符时,你应该使用(...)来明确优先级。

The GetBit() implementation is wrong, you can check with the following test: GetBit()实现是错误的,您可以通过以下测试检查:

[TestMethod]
public void MyTestMethod()
{
    var byte22And23 = new byte[] { 2, 0 };
    var sb = new StringBuilder();
    for (int i = 7; i >=0; i--)
    {
        var r = GetBit(byte22And23[0], i);
        sb.Append((r) ? "1" : "0");
    }

    // result: 00000100
    Assert.AreEqual("00000010", sb.ToString());
}

It looks like there is no need of -1 in GetBit() because you are indexing bits 0-based and not 1-based:看起来GetBit()中不需要-1 ,因为您正在索引基于 0 而不是基于 1 的位:

public bool GetBit(byte b, int bitNumber)
{
    // no need of -1 here ------------ˇ
    bool bit = (b & (1 << bitNumber - 1)) != 0;
    return bit;
}

After the modification the tests runs green.修改后,测试运行绿色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM