C#从字节数组中提取位范围

时间:2019-04-03 15:40:49

标签: c# .net bit-shift

我需要从16个字节的值中提取一些位范围,例如:

bit 0 = first thing
next 54 bits = second thing
next 52 bits = third thing
last 21 bits = fourth thing

.net没有UInt128结构,但是它具有BigInteger类,但是我不确定这是否合适,也许是这样?

我发现了一个第三方库,可以从流中读取位,但是当尝试使用UInt64将它们转换回BitConverter时,它将失败,因为54位是'对于UInt64来说足够长,但是对于UInt32

来说太长了

我立即想到的是移位的方法,但是现在我不确定如何进行处理,因为我想不出一种处理原始16字节的好方法。

任何建议或评论将不胜感激。

1 个答案:

答案 0 :(得分:0)

这是一些未经测试的代码。我确定其中存在错误(每当我编写这样的代码时,都会出现班次,蒙版等错误)。但是,这应该足以让您入门。如果您能正常工作,并且只有几个问题,请在评论中让我知道,我会解决问题。如果您无法使用它,也请告诉我,我将删除答案。如果需要进行重大重写,请将您的工作代码发布为答案,并告诉我。

与此有关的另一件事(因为您提到它来自文件)是 endian-ness 。并非所有的计算机体系结构都以相同的方式表示值。我会把所有字节留给您( swizzling (如果需要的话)。

首先,C ++中的结构与类基本相同(尽管人们认为它们是不同的)。在C#中,它们非常不同。 C#中的结构是值类型。当您进行值类型分配时,编译器将复制该结构的值,而不是仅复制该对象的引用(就像对类一样)。值类型具有一个隐式默认构造函数,该构造函数将所有成员初始化为其默认值(零或null)。

[StructLayout(LayoutKind.Sequential)]标记结构可以告诉编译器按照指定的顺序布局成员(它们通常不需要编译器)。如果需要,这允许您将对其中一个的引用(通过 P / Invoke )传递给C程序。

所以,我的结构以这种方式开始:

[StructLayout(LayoutKind.Sequential)]
public struct Struct128
{
    //not using auto-properties with private setters on purpose.
    //This should look like a single 128-bit value (in part, because of LayoutKind.Sequential)
    private ulong _bottom64bits;
    private ulong _top64bits;
}

现在,我要向该结构添加成员。由于您是从文件中获取128位的,因此请勿尝试将数据读入单个128位结构中(如果您可以弄清楚如何(查找序列化),则可以,但是...)。而是一次读取64位,并使用像这样的构造函数:

 public Struct128(ulong bottom64, ulong top64)
 {
     _top64bits = top64;
     _bottom64bits = bottom64;
 }

如果您需要将其中之一的数据写回到文件中,请使用如下所示的只读属性一次将其获取64位:

//read access to the raw storage
public ulong Top64 => _top64bits;
public ulong Bottom64 => _bottom64bits;

现在,我们需要从结构中获取并设置各种位值。掌握(设置)第一件事很容易:

public bool FirstThing
{
    get => (_bottom64bits & 0x01) == 1;
    set
    {
        //set or clear the 0 bit
        if (value)
        {
            _bottom64bits |= 1ul;
        }
        else
        {
            _bottom64bits &= (~1ul);
        }
    }
}

获取/设置第二和第四件事非常相似。在这两种情况下,要获得该值,都应屏蔽掉除重要位以外的所有位,然后移位结果。要设置该值,请获取属性值,将其移到正确的位置,将结构中存储的适当(顶部或底部)值中的位清零,或将新位(通过移位设置)中的OR清零。 / p>

//bits 1 through 55
private const ulong SecondThingMask = 0b111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1110;

public ulong SecondThing
{
    get => (_bottom64bits & SecondThingMask) >> 1;
    set
    {
        var shifted = (value << 1) & SecondThingMask;
        _bottom64bits = (_bottom64bits & (~SecondThingMask)) | shifted;
    }
}

 //top 21 bits
 private const ulong FourthThingMask = 0b1111_1111_1111_1111_1111_1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000;
 //to shift the top 21 bits down to the bottom 21 bits, need to shift 64-21
 private const int FourthThingShift = 64 - 21;

 public uint FourthThing
 {
     get => (uint)((_top64bits & FourthThingMask) >> FourthThingShift);
     set
     {
         var shifted = ((ulong)value << FourthThingShift) & FourthThingMask;
         _top64bits = (_top64bits & (~FourthThingMask)) | shifted;
     }
 }

这是棘手的第三件事。要获得该值,您需要屏蔽顶部值和底部值中的正确位,将它们移至正确位置并返回ORed结果。

要设置该值,您需要获取属性值,将其分为上下两部分,然后执行与第二和第四件事相同的魔术或运算:

 //the third thing is the hard part.  
 //The bottom 55 bits of the _bottom64bits are dedicate to the 1st and 2nd things, so the next 9 are the bottom 9 of the 3rd thing
 //The other 52-9 (=43) bits come-from/go-to the _top64bits

 //top 9 bits
 private const ulong ThirdThingBottomMask = 0b1111_1111_1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000;
 //bottom 43 bits
 private const ulong ThirdThingTopMask = 0b111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111;
 private const int ThirdThingBottomShift = 64 - 9;

 //bottom 9 bits
 private const ulong ThirdThingBottomSetMask = 0b1_1111_1111;
 //all but the bottom 9 bits
 private const ulong ThirdThingTopSetMask = 0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1110_0000_0000;
 //52 bits total
 private const ulong ThirdThingOverallMask = 0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111;

 public ulong ThirdThing
 {
     get
     {
         var bottom = (_bottom64bits & ThirdThingBottomMask) >> ThirdThingBottomShift;
         var top = (_top64bits & ThirdThingTopMask) << 9;
         return top | bottom;
     }
     set
     {
         var masked = value & ThirdThingOverallMask;
         var bottom = (masked & ThirdThingBottomSetMask) << ThirdThingBottomShift;
         _bottom64bits = (_bottom64bits & (~ThirdThingBottomSetMask)) | bottom;
         var top = (masked & ThirdThingTopSetMask) >> 9;
         _top64bits = (_top64bits & (~ThirdThingTopSetMask)) | top;
     }
 }

我希望这是有用的。让我知道。

相关问题