After reading the “interview question of the day” I stumbled upon an unfamiliar programming syntax regarding the xor operation. Can and will someone explain what goes on when executing:
result = 2;
out.println(result ^= 10); // This gives a result of 8.
result = 3;
out.println(result ^= 10); // This gives a result of 9.
result = 4;
out.println(result ^= 10); // This gives a result of 14.
I am having a hard time figuring this one out….
That is called a Bitwise XOR operator. It is 1 if EITHER of the compared bits are 1. If BOTH are 1, or BOTH are 0, the result will be 0. It is actually working on the BIT level of the numbers you are comparing.
Example: