In C and C++, the results of boolean operators like ==
, <
, can be used as 1
or 0
in arithmetic.
For example, a == b
results in 1
if a and b are equal, otherwise 0
.
?inline
if (condition) a++;
// is equivalent to
a += condition;
?inline
if (condition) a += b;
// is equivalent to
a += condition * b;
int compare(int a, int b) {
return (a > b) - (a < b);
}
int signum(int a) {
return (a > 0) - (a < 0); // cmp(a, 0)
}
There are many more examples where these branchless operations are useful. They can sometimes be more efficient than using an if-statement, but often, modern compilers transform code into these branchless versions automatically, so there is no difference.