tags:

views:

905

answers:

2

When I conpile this code:

BitArray bits = new BitArray(3);
bits[0] = true;
bits[1] = true; 
bits[2] = true;

BitArray moreBits = new BitArray(3);
bits[0] = true;
bits[1] = true;
bits[2] = true;

BitArray xorBits = bits.Xor(moreBits);

foreach (bool bit in xorBits)
{
Console.WriteLine(bit);
}

I get the following output:

True True True

When I do an xor on two boolean values by saying true ^ true i get false.

Is there something wrong with the code. My memory of the truth table for XOR was that True XOR True is false.

+22  A: 

Copy and paste error.

BitArray moreBits = new BitArray(3);
bits[0] = true;
bits[1] = true;
bits[2] = true;

That should be:

BitArray moreBits = new BitArray(3);
moreBits[0] = true;
moreBits[1] = true;
moreBits[2] = true;

HTH, Kent

Kent Boogaart
+4  A: 

You are setting bits to true twice. You are not settings moreBits to true, so it defaults to all-false. I blame copy/paste!

EDIT: in the short time it took me to write this Kent answered and got upvoted 8 times!

Lucas
+1... awww.. we've all been there dude :)
Dead account