Is there something wrong with BitArrays in C #?

When I compromise 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 xor over two booleans, saying true, true, I get false.

Something is wrong with the code. My truth table memory for XOR was that True XOR True is false.

+3
source share
2 answers

Copy and paste the error.

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

It should be:

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

You install bitsin truetwice. You are not setting moreBitsup true, so the default value is all- false. I blame copy / paste!

: , !

+6

Source: https://habr.com/ru/post/1704648/


All Articles