473,388 Members | 1,327 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 3822
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ramzi Abboud | last post by:
I want to store a binary string in SqlServer from VB .NET through a stored procedure. I have been storing the binary data in a BitArray which I am not set on by any means. When I pass the...
2
by: Joel Moore | last post by:
Maybe I'm just easily baffled after an all-nighter but I can't seem to figure out how to represent a BitArray as a hexadecimal string. For example: Dim outputBank As New BitArray(8) ...
1
by: Marc Lefebvre | last post by:
How can I convert an Int to a BitArray ? Or How can I format an Int to a binairy string ? Thank's Marc Lefebvre
3
by: Laszlo Szijarto | last post by:
In using the BitArray class to parse a single byte, I noticed that the bits show up in Little Endian order. Aren't bits always arranged in Big Endian order? I'm a bit confused and would...
7
by: Eric | last post by:
Hi All, I need to XOR two same-length Strings against each other. I'm assuming that, in order to do so, I'll need to convert each String to a BitArray. Thus, my question is this: is there an...
0
by: James | last post by:
Hi, I need to use an array of bitarray and being able to dinamically redim the bitarray and the array of bitarray. I need something like: structure stBarray public BA as bitarray()...
3
by: HKannen | last post by:
Hello Everybody, I wrote a little method that gets me an Int32 from a BitArray. This method looks like this: private Int32 GetIntVal( BitArray iArray ) { Byte lByteArr = new Byte; ...
7
by: Rick Williams | last post by:
I was so happy to find the BitArray class. Until I 'cut and pasted' the following sample code from Visual Studio's help: #using <mscorlib.dll> #using <system.dll> using namespace System;...
2
by: semedao | last post by:
Hi , I try the BitArray class to make Xor on 2 byte arrays. The result was that when I Xor 1with 2 I get 3 which is correct then Xoring 3 with 1 give me 0 (zero) when it should give me 2 ! ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.