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
 
 
 
 Omar Kooheji 
 
 55.9k71 gold badges187 silver badges243 bronze badges
 
 - 
 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.Keith– Keith2009年03月12日 13:32:04 +00:00Commented 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.Omar Kooheji– Omar Kooheji2009年03月12日 14:43:27 +00:00Commented Mar 12, 2009 at 14:43
- 
 1Why is this getting up voted?Omar Kooheji– Omar Kooheji2009年03月12日 15:06:07 +00:00Commented Mar 12, 2009 at 15:06
- 
 you can close your own questionFredou– Fredou2009年03月12日 15:44:15 +00:00Commented 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...Omar Kooheji– Omar Kooheji2009年03月12日 16:12:32 +00:00Commented Mar 12, 2009 at 16:12
2 Answers 2
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
 
 
 
 Kent Boogaart 
 
 179k39 gold badges400 silver badges397 bronze badges
 
 
 Sign up to request clarification or add additional context in comments.
 
 
 
 1 Comment
Eight-Bit Guru
 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. ;)
  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
 
 
 
 Lucas 
 
 17.5k5 gold badges48 silver badges41 bronze badges
 
 Comments
lang-cs