473,667 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BitArray bug or I am confused?

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

BitArray ba1, ba2, ba3 , ba4;

Int64 i1,i2,i3,i4;

i1 = 1;

i2 = 2;

byte[] b1,b2,b3,b4;

b1 = BitConverter.Ge tBytes(i1);

b2 = BitConverter.Ge tBytes(i2);

ba1 = new BitArray(b1);

ba2 = new BitArray(b2);

b3 = new byte[Math.Max(b1.Len gth, b2.Length)];

b4 = new byte[Math.Max(b1.Len gth, b2.Length)];
ba3 = ba1.Xor(ba2);

ba3.CopyTo(b3, 0);

String s1 = BitConverter.To Int64(b3, 0).ToString();

BitArray ba4 = ba3.Xor(ba1);

ba4.CopyTo(b4, 0);

String s2 = BitConverter.To Int64(b4,0).ToS tring();

ba4 = ba3.Xor(ba2);

ba4.CopyTo(b4, 0);

String s3 = BitConverter.To Int64(b4, 0).ToString();
Jan 8 '07 #1
2 2839
semedao <se*****@commun ity.nospamwrote :
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)
<snip>

Okay, well, let's make your sample code a bit more manageable. There's
no need to pepper the code with conversions and things, when you can do
them once:
using System;
using System.Collecti ons;

class Test
{
static void Main()
{
BitArray ba1 = new BitArray(new int[]{1});
BitArray ba2 = new BitArray(new int[]{2});
BitArray ba3 = ba1.Xor(ba2);

DumpBitArray(ba 3);
DumpBitArray(ba 3.Xor(ba1));
DumpBitArray(ba 3.Xor(ba2));
}

static void DumpBitArray(Bi tArray bitArray)
{
byte[] bytes = new byte[(bitArray.Lengt h+7)/8];
bitArray.CopyTo (bytes, 0);
Console.WriteLi ne (BitConverter.T oString(bytes)) ;
}
}

That prints:

03-00-00-00
00-00-00-00
02-00-00-00

And that demonstrates the problem you're having. Now we can easily add
another couple of lines to look at ba1 and ba2, just to check they
haven't changed at the end

DumpBitArray(ba 1);
DumpBitArray(ba 2);

Now the full dump is:

03-00-00-00
00-00-00-00
02-00-00-00
02-00-00-00
02-00-00-00

ba1 appears to have changed! Given that all we've done is call Xor on
it, we need to consult the documentation for Xor... unfortunately, the
docs don't make it terribly clear, although the example *does* show
that calling Xor on a BitArray changes its contents.

If you don't want to change the contents, you could either call Clone
on the original or create a new one with new BitArray (oldBitArray).
For instance:

BitArray ba3 = new BitArray(ba1).X or(ba2);

So the documentation could certainly be a lot clearer, but the method
does work.

--
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 8 '07 #2
thanks
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
semedao <se*****@commun ity.nospamwrote :
>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)

<snip>

Okay, well, let's make your sample code a bit more manageable. There's
no need to pepper the code with conversions and things, when you can do
them once:
using System;
using System.Collecti ons;

class Test
{
static void Main()
{
BitArray ba1 = new BitArray(new int[]{1});
BitArray ba2 = new BitArray(new int[]{2});
BitArray ba3 = ba1.Xor(ba2);

DumpBitArray(ba 3);
DumpBitArray(ba 3.Xor(ba1));
DumpBitArray(ba 3.Xor(ba2));
}

static void DumpBitArray(Bi tArray bitArray)
{
byte[] bytes = new byte[(bitArray.Lengt h+7)/8];
bitArray.CopyTo (bytes, 0);
Console.WriteLi ne (BitConverter.T oString(bytes)) ;
}
}

That prints:

03-00-00-00
00-00-00-00
02-00-00-00

And that demonstrates the problem you're having. Now we can easily add
another couple of lines to look at ba1 and ba2, just to check they
haven't changed at the end

DumpBitArray(ba 1);
DumpBitArray(ba 2);

Now the full dump is:

03-00-00-00
00-00-00-00
02-00-00-00
02-00-00-00
02-00-00-00

ba1 appears to have changed! Given that all we've done is call Xor on
it, we need to consult the documentation for Xor... unfortunately, the
docs don't make it terribly clear, although the example *does* show
that calling Xor on a BitArray changes its contents.

If you don't want to change the contents, you could either call Clone
on the original or create a new one with new BitArray (oldBitArray).
For instance:

BitArray ba3 = new BitArray(ba1).X or(ba2);

So the documentation could certainly be a lot clearer, but the method
does work.

--
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 9 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4018
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 obviously does not implement IConvertable. If anyone has a good way of passing binary data into...
2
8925
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
5177
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
9897
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
7589
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 idea of how to go about this they may (hopefully) bring that up...?
0
1165
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
2202
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 );
4
3846
by: Rainer Queck | last post by:
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++){
7
2421
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;
0
8367
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8889
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8790
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8570
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8650
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7391
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4202
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2781
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2017
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.