BitArray bug or I am confused?

S

semedao

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.GetBytes(i1);

b2 = BitConverter.GetBytes(i2);

ba1 = new BitArray(b1);

ba2 = new BitArray(b2);

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

b4 = new byte[Math.Max(b1.Length, b2.Length)];


ba3 = ba1.Xor(ba2);

ba3.CopyTo(b3, 0);

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

BitArray ba4 = ba3.Xor(ba1);

ba4.CopyTo(b4, 0);

String s2 = BitConverter.ToInt64(b4,0).ToString();

ba4 = ba3.Xor(ba2);

ba4.CopyTo(b4, 0);

String s3 = BitConverter.ToInt64(b4, 0).ToString();
 
J

Jon Skeet [C# MVP]

semedao said:
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.Collections;

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(ba3);
DumpBitArray(ba3.Xor(ba1));
DumpBitArray(ba3.Xor(ba2));
}

static void DumpBitArray(BitArray bitArray)
{
byte[] bytes = new byte[(bitArray.Length+7)/8];
bitArray.CopyTo(bytes, 0);
Console.WriteLine (BitConverter.ToString(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(ba1);
DumpBitArray(ba2);

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).Xor(ba2);

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

semedao

thanks
Jon Skeet said:
semedao said:
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.Collections;

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(ba3);
DumpBitArray(ba3.Xor(ba1));
DumpBitArray(ba3.Xor(ba2));
}

static void DumpBitArray(BitArray bitArray)
{
byte[] bytes = new byte[(bitArray.Length+7)/8];
bitArray.CopyTo(bytes, 0);
Console.WriteLine (BitConverter.ToString(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(ba1);
DumpBitArray(ba2);

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).Xor(ba2);

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top