471,319 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

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 ;
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
Jan 18 '06 #1
4 3697
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 Queck" <Ra****@noemail.noemail> wrote in message
news:ux****************@TK2MSFTNGP10.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 ;
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

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.com

"Rainer Queck" <Ra****@noemail.noemail> wrote in message
news:ux****************@TK2MSFTNGP10.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 ;
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

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 ;
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Ramzi Abboud | last post: by
2 posts views Thread by Joel Moore | last post: by
1 post views Thread by Marc Lefebvre | last post: by
3 posts views Thread by Laszlo Szijarto | last post: by
7 posts views Thread by Eric | last post: by
reply views Thread by James | last post: by
3 posts views Thread by HKannen | last post: by
7 posts views Thread by Rick Williams | last post: by
2 posts views Thread by semedao | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.