PC Review


Reply
Thread Tools Rate Thread

BitArray bug or I am confused?

 
 
semedao
Guest
Posts: n/a
 
      8th Jan 2007
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();


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      8th Jan 2007
semedao <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
semedao
Guest
Posts: n/a
 
      9th Jan 2007
thanks
"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> semedao <(E-Mail Removed)> wrote:
>> 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.
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BitArray -> int Alexander Vasilevsky Microsoft C# .NET 2 28th Nov 2011 05:25 AM
Help: BitArray James Microsoft VB .NET 0 14th Nov 2005 05:11 PM
Convert int to BitArray Glen Wilkin via DotNetMonster.com Microsoft C# .NET 2 21st Sep 2005 11:32 AM
BitArray vs. BitVector32 =?Utf-8?B?Y2hveWsx?= Microsoft Dot NET Framework 0 29th Jan 2005 12:45 AM
How can I convert an Int to a BitArray ? Marc Lefebvre Microsoft C# .NET 1 11th Sep 2003 07:07 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:11 AM.