c# - Last two bits in a BitArray of length 4 are being ignored when converting to int -
consider following bitarray:
bitarray bitarray = new bitarray(new boolean[] {true, true, false, false}); and in binary equals:
1100 now want convert int , have tried use methods described on page: how can convert bitarray single int?
however, both these methods converts 1100 3 instead of 12. seems if ignores last 2 bits , considers of size 2 bit, of course answer 3.
one of methods on linked page above, in action:
int[] array = new int[1]; bitarray.copyto(array, 0); after executing above, bitarray has value 3.
how can express in code want consider 4 bits?
the constructor bitarray(bool[]) accepts values in index order - , copyto uses them in traditional significance (so bitarray[0] least significant bit) - true, true, false, false ends meaning 0011 in binary, not 1100.
it's not ignoring last 2 bits - it's treating initial array in opposite order 1 expected.
if want make first-specified value most significant value when converting bits integers, you'll need reverse input array.
Comments
Post a Comment