如何从字节读取前4位和后4位?

时间:2013-04-09 10:36:29

标签: .net byte bits

C#如何从字节读取前4位和后4位?

4 个答案:

答案 0 :(得分:25)

按位AND使用并移位,如下所示:

byte b = 0xAB;
var low = b & 0x0F;
var high = b >> 4;

答案 1 :(得分:2)

我更愿意使用它 -

byte a = 68;
byte high_bits = a>>4;
byte low_bits = a&15;

答案 2 :(得分:1)

在一个方便的结构中:

用法

var hb = new HalvedByte(5, 10);
hb.Low -= 3;
hb.High += 3;
Console.Write(string.Format("{0} / {1}", hb.Low, hb.High));
// 2, 13

代码

public struct HalvedByte
{
    public byte Full { get; set; }

    public byte Low
    {
        get { return (byte)(Full & 0x0F); }

        set
        {
            if (value >= 16)
            {
                throw new ArithmeticException("Value must be between 0 and 16."); 
            }

            Full = (byte)((High << 4) | (value & 0x0F));
        }
    }

    public byte High
    {
        get { return (byte)(Full >> 4); }

        set
        {
            if (value >= 16)
            {
                throw new ArithmeticException("Value must be between 0 and 16.");
            }

            Full = (byte)((value << 4) | Low);
        }
    }

    public HalvedByte(byte full)
    {
        Full = full;
    }

    public HalvedByte(byte low, byte high)
    {
        if (low >= 16 || high >= 16)
        {
            throw new ArithmeticException("Values must be between 0 and 16.");
        }

        Full = (byte)((high << 4) | low);
    }
}

奖励:数组(未经测试)

如果您需要使用这些半字节的数组,这将简化访问:

public class HalvedByteArray
{
    public int Capacity { get; private set; }
    public HalvedByte[] Array { get; private set; }

    public byte this[int index]
    {
        get
        {
            if (index < 0 || index >= Capacity)
            {
                throw new IndexOutOfRangeException();
            }

            var hb = Array[index / 2];

            return (index % 2 == 0) ? hb.Low : hb.High;
        }
        set
        {
            if (index < 0 || index >= Capacity)
            {
                throw new IndexOutOfRangeException();
            }

            var hb = Array[index / 2];

            if (index % 2 == 0)
            {
                hb.Low = value;
            }
            else
            {
                hb.High = value;
            }
        }
    }

    public HalvedByteArray(int capacity)
    {
        if (capacity < 0)
        {
            throw new ArgumentException("Capacity must be positive.", "capacity");
        }

        Capacity = capacity;
        Array = new HalvedByte[capacity / 2];
    }
}

答案 3 :(得分:0)

简单的方法。

示例用法:

A:10101110

B:00001110

从A获取最后4位,因此输出类似于B

getBitRange(10101110,0,4);

//gets a bits from a byte, and return a byte of the new bits
byte getBitRange(byte data, int start, int _end){

//shift binary to starting point of range
byte shifted = (data >> start);

//calculate range length (+1 for 0 index)
int rangeLength = (_end-start)+1;

//get binary mask based on range length 
byte maskBinary;
 
switch (rangeLength){
case 1: maskBinary = 0b00000001; break;
case 2: maskBinary = 0b00000011; break;
case 3: maskBinary = 0b00000111; break;
case 4: maskBinary = 0b00001111; break;
case 5: maskBinary = 0b00011111; break;
case 6: maskBinary = 0b00111111; break;
case 7: maskBinary = 0b01111111; break;
case 8: maskBinary = 0b11111111; break;
default:
  // default statements
  Serial.println("Error: Range length too long!!");
}

//cancel out 
byte finalByte = (shifted & maskBinary);

return finalByte;
}