How can this code be simplified?
if (x == 0) x = 1;
else if (x == 1) x = 0;
else if (x == 2) x = 3;
else if (x == 3) x = 2;
If x is always between 0 and 3 then try this:
x ^= 1;
It toggles the least significant bit.
If x can be a value other than between 0 to 3 then you can first test for that:
if (x >= 0 && x <= 3) {
x ^= 1;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With