Let a, b be positive integers with different values. Is there any way to simplify these expressions:
bool foo(unsigned a, unsigned b)
{
if (a % 2 == 0)
return (b % 2) ^ (a < b); // Should I write "!=" instead of "^" ?
else
return ! ( (b % 2) ^ (a < b) ); // Should I write "(b % 2) == (a < b)"?
}
I am interpreting the returned value as a boolean.
How is it different from
which in turn is
or, indeed
Edited to add: Thinking about it some more, this is just the xor of the first and last bits of
(a-b)(if you use 2’s complement), so there is probably a machine-specific ASM sequence which is faster, involving a rotate instruction.