BitArray.Length似乎无法正常工作 [英] BitArray.Length doesn't seem to be working properly

查看:185
本文介绍了BitArray.Length似乎无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该能够使用长度通过减小长度来有效地左移位.

但是,当我更改长度时,计数"和长度"值会更改,但是BitArray中的位值保持不变.

有没有人看到这种行为?

我需要能够提取一个大字节数组并将左侧的位刮掉.它还需要高效.因此,如果有人能想到更好的解决方案,我也很乐意.

谢谢
詹姆斯

[更新]

我可能应该提到我正在使用Visual Studio 9运行Windows XP.

Should be able to use the Length to effectively left shift the bits by reducing the length.

However, when i change the length the Count and Length values change, but the bit values in the BitArray remain the same.

does anyone see this behavior?

I need to be able to take a large byte array and shave bits off the left side. It also needs to be efficient. So, if anyone could think of a better solution I''m open to that as well.

Thanks
James

[Update]

I should probably mention I''m running Windows XP, with Visual Studio 9.

推荐答案

如果您使用CopyTo 取回字节,那么它将赢得不能按您认为的那样工作,因为它将使用内部值作为原始值(在减小Length之前).我不确定这是错误还是特意设计,因为绝对没有关于该如何工作的文档.


-----------

在这里也添加我的评论,因为它添加了答案:

首先,该文档具有误导性.不是删除的第0个元素,而是第(Length-1)个元素,因为BitArray反向存储一个字节.

其次,也许作为代码优化的一部分,如果减小了Length ,那么它就不会麻烦重置内部数组的值,因为无论如何您都无法访问Length 之后的元素.

具有讽刺意味的是,CopyTo忽略了这一点.因此,从这个方面来说,我会说这是一个错误,但是考虑到这是部分未定义的行为,那么编写此类的任何人都可以声称它不是错误,而是未定义的行为.

我知道这没有帮助您解决最初的问题,但我只是告诉您该类当前的工作方式.
If you are using CopyTo to get back the byte, then it won''t work as you think it will since it will use the internal value which will be the original value (before you reduced the Length). I am not sure if that''s a bug or by design since there''s absolutely no documentation on how that''s supposed to work.


-----------

Adding my comment here too, since it adds to the answer:

First of all, that documentation is misleading. It''s not the 0th element that''s deleted, it''s the (Length - 1)th element, since the BitArray stores a byte in reverse.

And secondly, perhaps as part of a code optimization, if the Length is reduced, then it does not bother resetting the internal array value since you can''t access the element past the Length anyway.

Ironically, CopyTo ignores this. So from that aspect I would say this is a bug, but considering that this is partially undefined behavior, then whoever wrote this class can claim that it''s not a bug and rather it''s undefined behavior.

I know this is not helping your original problem, but I am just telling you how the class works currently.


这里是使用常规移位的另一种方法:

Here''s an alternate way to do this using regular bit-shifting:

static void Main(string[] args)
{

    byte b = 255;
    Show(b);//255
    b = ShaveLeftBit(b);
    Show(b);//127
    b = ShaveLeftBit(b);
    Show(b);//63
    b = ShaveLeftBit(b);
    Show(b);//31
    b = ShaveLeftBit(b);
    Show(b);//15
}

private static byte ShaveLeftBit(byte b)
{
   return (byte) (b >> 1);
}

private static void Show(byte b)
{
    Console.WriteLine(b);
}


这篇关于BitArray.Length似乎无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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