BitArray question

  • Thread starter Thread starter Rainer Queck
  • Start date Start date
R

Rainer Queck

Hi NG

I have some questions concerning BitArrays.
Assumption : BitArray with 16 Bits

Is it possible to "load" a BitArray with a UInt16 Value with out iterating
it like:

UInt16 Bits = 0xAA55;
for (int i = 0 ; i<16 ; i++){
MyBitArray = (Bits & Mask) >0 ;
Mask <<= 1;
}

and then, is there a way to "read" a BitArray like (without iterating like
above)
UInt16 Bits = MyBitArray.<some read method> ?

Thanks for hints and help.

Regards
Rainer
 
There several constructors for BitArray

public BitArray(byte[]);
public BitArray(int);

maybe you can do something with them?

E.g:
UInt16 Bits = 0xAA55;
BitArray arr = new BitArray(new byte [2] {(byte)(Bits), (byte)(Bits >> 8)});


Just speculating...Efficient - don't know...

cheers,
mortb
 
Rainer,

Use the constructor that takes a byte array, and do this:

byte[] Bits = new byte[2]{0xAA, 0x55};
BitArray MyBitArray = new BitArray(Bits);

As for reading from the bit array, there doesn't seem to be an easy way
to do it, short of iterating through the flags and setting each bit (similar
to the operation you used to populate the bit array).

Hope this helps.
 
Hi mortb, hi Nicholas,

I was aware of the possibility to use the BitArray constructor.
But I was looking for a "easy" was to assign WORD or BYTE values to a
existing instance of a BitArray.

Well, it looks like to have to go through iterations for my needs.
Any how, thanks for your respons.

Regards
Rainer
 
Rainer Queck said:
Hi NG

I have some questions concerning BitArrays.
Assumption : BitArray with 16 Bits

Is it possible to "load" a BitArray with a UInt16 Value with out iterating
it like:

UInt16 Bits = 0xAA55;
for (int i = 0 ; i<16 ; i++){
MyBitArray = (Bits & Mask) >0 ;
Mask <<= 1;
}

and then, is there a way to "read" a BitArray like (without iterating like
above)
UInt16 Bits = MyBitArray.<some read method> ?


No, but you can use BitArray.CopyTo to copy to an array (of int rather
than uint, admittedly).
 
Back
Top