BitArray - 换档位档位、BitArray

由网友(比夏天还要温暖的、女子)分享简介:我有一个System.Collections.BitArray阵列(〜3000项),我想所有的位左移1。然而,收集似乎并不支持这种操作(即bitArray<< 1不工作,并且没有方法)。如何做到这一点任何想法?I have a System.Collections.BitArray array (~300...

我有一个System.Collections.BitArray阵列(〜3000项),我想所有的位左移1。然而,收集似乎并不支持这种操作(即bitArray<< 1不工作,并且没有方法)。如何做到这一点任何想法?

I have a System.Collections.BitArray array (~3000 items) and I would like to shift all the bits to the left by 1. However the collection doesn't seem to support that operation (i.e. bitArray << 1 not working and there is no method). Any idea on how to do that?

谢谢!

推荐答案

这个简单的代码片段显示了一个人工的方式来做到这一点。 bitArray的值[0] 被覆盖:

This simple snippet shows a manual way to do it. The value of bitArray[0] is overwritten:

//... bitArray is the BitArray instance

for (int i = 1; i < bitArray.Count; i++)
{
   bitArray[i - 1] = bitArray[i];
}

bitArray[bitArray.Count - 1] = false // or true, whatever you want to shift in

使之成为一个扩展方法不应该是一个大问题。

Making this an extension method shouldn't be a big deal.

阅读全文

相关推荐

最新文章