As part of my CS classes I've recently completed the pretty popular "Data Lab" assignments. In these assignments you are supposed to implement simple binary operations in C with as few operations as possible.
For those who are not familiar with the "Data Lab" a quick overview about the rules:
- You may not call functions, cast or use control structures (e.g. if)
- You may assign variables with no operator cost, however only int is allowed)
- The less operators you use, the better
- You may assume sizeof(int) == 32
- Negative numbers are represented in 2's complement
The task is to implement a logical not called 'bang' (where bang(x) returns !x) by only using the following operators: ~ & ^ | + << >>
The function prototype is defined as
int bang(int x)
The best implementation I could find (using 5 operators) was the following:
return ((x | (~x +1)) >> 31) + 1
However there seems to be a way to accomplish this with even less operators, since I found a result website[1] from some German university where two people apparently found a solution with less than 5 operator. But I can't seem to figure out how they accomplished that.
[1] http://rtsys.informatik.uni-kiel.de/~rt-teach/ss09/v-sysinf2/dlcontest.html (logicalNeg column)
To clarify: This is not about how to solve the issue, but how to solve it with less operations.