When doing some bit twiddling and removing branches the following problem pops up from time to time:
Say I have an integer binary flag f, which can be 0 or 1. How can I turn this into respectively -1 and 1 (or the opposite way around, at your discretion – often either one is usable). A common way is this:
int i = 2*f - 1;
But this uses an addition and a multiplication. Can it be done any faster?
Well, for starters you can do
Also, you could try