It's easy to to change +1 to -1 and 0 to 1 with an expression like:
value = (value ? 0 : 1) - value;
But that introduces a branch. Is there a bitwise way to perform the same expression?
It's easy to to change +1 to -1 and 0 to 1 with an expression like:
value = (value ? 0 : 1) - value;
But that introduces a branch. Is there a bitwise way to perform the same expression?
Sure you could use boring old math, but why not use something weirder:
const int map[] = { 1, -1 };
thus,
map[1] yields -1
map[0] yields 1
so you'd write:
value = map[value];
value = 1 - 2*value;
maps 0
to 1
and +1
to -1
as requested.