The main idea (I suppose) would be to change something like:
if (a>b) return "A is greater than B"; else return "A is less than or equal to B";
in
static char const *strings[] = { "A is less than or equal to B", "A is greater than B" }; return strings[a>b];
For branches in binary search, consider the basic idea of โโa โnormalโ binary search, which usually looks (at least indefinitely) as follows:
while (left < right) { middle = (left + right)/2; if (search_for < array[middle]) right = middle; else left = middle; }
We could get rid of the branch here in much the same way:
size_t positions[] = {left, right}; while (left < right) { size_t middle = (left + right)/2; positions[search_for >= array[middle]] = middle; }
[For general purpose code, use left + (right-left)/2 instead of (left+right)/2 ]
source share