I'm trying to initialize a System.BitArray instance from integer value. However, it looks like I don't get the right values.
My code is
var b = new BitArray(BitConverter.GetBytes(0xfa2));
for (int i = 0; i < b.Count; i++)
{
char c = b[i] ? '1' : '0';
Console.Write(c);
}
Console.WriteLine();
I've tried also without BitConverter:
var b = new BitArray(new int[] { 0xfa2 });
But none of these attempt seem to work. These are the attempts that was suggested here: Convert int to a bit array in .NET
My output: 01000101111100000000000000000000. The excpected output: 111110100010.
Any help will be really appreciated!