473,543 Members | 2,446 Online

# BitArray question

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[i] = (Bits & Mask) >0 ;
}

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

Thanks for hints and help.

Regards
Rainer
Jan 18 '06 #1
4 3832
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...E fficient - don't know...

cheers,
mortb

"Rainer Queck" <Ra****@noemail .noemail> wrote in message
news:ux******** ********@TK2MSF TNGP10.phx.gbl. ..
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[i] = (Bits & Mask) >0 ;
}

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

Thanks for hints and help.

Regards
Rainer

Jan 18 '06 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Rainer Queck" <Ra****@noemail .noemail> wrote in message
news:ux******** ********@TK2MSF TNGP10.phx.gbl. ..
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[i] = (Bits & Mask) >0 ;
}

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

Thanks for hints and help.

Regards
Rainer

Jan 18 '06 #3
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
Jan 18 '06 #4
Rainer Queck <Ra****@noemail .noemail> wrote:
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[i] = (Bits & Mask) >0 ;
}

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

No, but you can use BitArray.CopyTo to copy to an array (of int rather

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet