473,508 Members | 2,312 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.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();
Jan 8 '07 #1
2 2832
semedao <se*****@community.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.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 - <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 8 '07 #2
thanks
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
semedao <se*****@community.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.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 - <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 9 '07 #3

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...
2
8898
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
5152
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
9845
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
7576
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
1161
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
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; ...
4
3830
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 =...
7
2396
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;...
0
7233
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
7342
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,...
1
7067
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...
0
7505
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...
1
5060
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...
0
4729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3215
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...
0
1570
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 ...
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.