-1

In C# I have a BitArray that stores a value of 5 as follows

BitArray bitArray = new BitArray(new int[] { 5});

I want to retrieve the value of 5 from the BitArray as an integer as follows:

int myInt = //some operation on bitArray goes here

What would be a fast method of retrieving it? This operation will be repeated heavily so performance is important.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Caustix
  • 569
  • 8
  • 26
  • The answer here has additional checks for length which I don't need https://stackoverflow.com/questions/5283180/how-can-i-convert-bitarray-to-single-int – Caustix Sep 18 '17 at 19:50

1 Answers1

2

Just copy the bit array to an int array and take the first element.

BitArray bitArray = new BitArray(new int[] { 5 });
int[] array = new int[1];
bitArray.CopyTo(array, 0);
int result = array[0];
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49