473,543 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.<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 ;
Mask <<= 1;
}

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 ;
Mask <<= 1;
}

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 ;
Mask <<= 1;
}

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
than uint, admittedly).

--
Jon Skeet - <sk***@pobox.co m>
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
4009
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 BitArray into a stored procedure, which is looking for a variable of type binary, it throws the following: "Object must implement IConvertible." BitArray...
2
8904
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) outputBank(0) = True outputBank(1) = False outputBank(2) = False
1
5159
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
9849
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 appreciate any assistance. Thank you, Laszlo
7
7578
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 easy way to convert a String to a BitArray (and back again)? I explained the ultimate goal (XORing two Strings) so that, if anyone has a better...
0
1162
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() endstructure dim Array() as stBarray so I can:
3
2194
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; iArray.CopyTo( lByteArr, 0 );
7
2402
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; using namespace System::Collections;
2
2834
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 ! but Xoring 3 with 2 give 2 which is correct... (should be 1) here is the sample code...
0
7402
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7590
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7733
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7344
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7684
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5264
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3388
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
963
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.