bitwise xor is giving the wrong result

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hi!

How on earth can the result on xorbits be true when you are doing an xor
between two
bits where each bit is true it should be false ?

public void Main(string[] args)
{
BitArray bits = new BitArray(1);
bits[0] = true;

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

BitArray xorbits = bits.Xor(morebits);

foreach (bool bit in xorbits)
Console.WriteLine(bit);
}

//Tony
 
Tony said:
Hi!

How on earth can the result on xorbits be true when you are doing an xor
between two
bits where each bit is true it should be false ?

Because you mistakenly initialized bits[0] to true twice, leaving
morebits[0] initialized to false, resulting in the evaluation
false xor true
 
Back
Top