How to work with the bits in a byte

前端 未结 4 851
不知归路
不知归路 2020-12-17 03:20

I have a single byte which contains two values. Here\'s the documentation:

The authority byte is split into two fields. The three least significant bits

4条回答
  •  被撕碎了的回忆
    2020-12-17 04:20

    Your use of the BitArray is incorrect. This:

    BitArray BA = new BitArray(mybyte);
    

    ..will be implicitly converted to an int. When that happens, you're triggering this constructor:

    BitArray(int length);
    

    ..therefore, its creating it with a specific length.

    Looking at MSDN (http://msdn.microsoft.com/en-us/library/x1xda43a.aspx) you want this:

    BitArray BA = new BitArray(new byte[] { myByte });
    

    Length will then be 8 (as expected).

提交回复
热议问题