[Solved] C# – fast way to compare 2 integers, bit by bit, and output for more than one integer, possible?


I hope I understood your question right.

You are looking for the XOR operator.

C = A ^ B

A 110101
B 101100
--------
C 011001

XOR will always be 1 if the two inputs are “different”. See:

| A | B | XOR |
|---+---+-----|
| 0 | 0 |  0  |
| 0 | 1 |  1  |
| 1 | 0 |  1  |
| 1 | 1 |  0  |

Then you will be able to loop through the bits of C like this:

for (int i = 0; i < 32; i++)
{
    bool bit = (C & (1 << i)) != 0;
}

4

solved C# – fast way to compare 2 integers, bit by bit, and output for more than one integer, possible?