Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BitArray - Shift bits

Tags:

c#

.net

bitarray

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?

Thanks!

like image 818
Martin Avatar asked Sep 10 '10 10:09

Martin


1 Answers

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.

like image 130
Frank Bollack Avatar answered Sep 30 '22 11:09

Frank Bollack