我有一个System.Collections.BitArray数组(大约3000个项目),我想把所有的位都向左移位1。但是这个集合似乎不支持这个操作(例如,bitArray << 1不工作,并且没有方法)。你知道该怎么做吗?
谢谢!
发布于 2010-09-10 19:09:51
这个简单的代码片段展示了一种手动执行此操作的方法。bitArray[0]的值将被覆盖:
//... 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让它成为一个扩展方法应该没什么大不了的。
发布于 2011-08-05 06:25:39
System.Numerics.BigInteger确实支持位移位。
发布于 2011-10-08 20:29:05
我不确定效率如何,但是这个扩展方法可以做到这一点
public static BitArray ShiftRight(this BitArray instance)
{
return new BitArray(new bool[] { false }.Concat(instance.Cast<bool>().Take(instance.Length - 1)).ToArray());
}https://stackoverflow.com/questions/3684002
复制相似问题