BitArray -> int

  • Thread starter Alexander Vasilevsky
  • Start date
J

Jon Skeet [C# MVP]

Alexander Vasilevsky said:
There are 32 - bit BitArray.

How cast this value to int?

Try this:

using System;
using System.Collections;

class Test
{
static void Main()
{
BitArray ba = new BitArray(32);
ba.Set(10, true);
ba.Set(0, true);

int[] array = new int[1];
ba.CopyTo(array, 0);

// Prints 1025 = 1<<10 | 1<<0
Console.WriteLine (array[0]);
}
}
 
Joined
Nov 28, 2011
Messages
1
Reaction score
0
I'm having trouble getting this to work. I thought I must have been doing this wrong, but after looking at the above answer I'm doing it exactly the same.

I;m actually setting up a bitarray that is all true, then eliminating each bit (setting to false) as I verify that it can't be true. The end result is either none are true, or only one is.
I have debugged out the values in the bitarray and that part of my function is working, there is one true and the rest false.
However when I use CopyTo to convert it into an int (using the int[1] method above) the int value is -524287

I don't know how it get's a negative, but interestingly 524287 would be the number I got from my bitarray if they were all set to true.

Any ideas on this one?

Cheers,
Adsy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top