4

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.

asked Mar 12, 2009 at 13:03
5
  • Frameworks like C#'s or Java's are almost never at fault because so many other people are using them and testing them. Always check your own code first. In this case Kent's answer covers it. Commented Mar 12, 2009 at 13:32
  • yeah I tried to delete the question once I'd noticed that but because his answer has been voted up I can't delete it. Somone else close it. Commented Mar 12, 2009 at 14:43
  • 1
    Why is this getting up voted? Commented Mar 12, 2009 at 15:06
  • you can close your own question Commented Mar 12, 2009 at 15:44
  • No you can votw to close it, but not close it... I need 3 more votes. and it's been upvoted again... I despair... do people actually read questions? At least Kent got a good answer badge for spotting my idiocy... Commented Mar 12, 2009 at 16:12

2 Answers 2

27

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;
answered Mar 12, 2009 at 13:06
Sign up to request clarification or add additional context in comments.

1 Comment

Been there, done that. And it's not an error you make once, early in your career, and never repeat - copy/paste gremlins lurk forever behind your monitor, just waiting for a momentary lapse in concentration. ;)
5

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!

answered Mar 12, 2009 at 13:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.