BitArray - 换档位 [英] BitArray - Shift bits

查看:165
本文介绍了BitArray - 换档位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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.

这篇关于BitArray - 换档位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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