I have an interesting puzzle. I need to convert from a signed number, say -5 (0x80000005) to two’s complement -5 (0xFFFFFFFB). The only operators I can use are, !, ~, &, ^, |, +, <<, >> and at most 15 of them in any combination with as many variables as I want. = may be used and does not count towards the total number of operators. Also, no “if” statements, loops or calls to functions of any kind may be used.
the function will look like
int sign2two(int x){
//... some stuff here
return x;
}
The problem I am having is that I can find out if the number is negative. But I want to say, if num is negative then flip bits and add 1. Otherwise return number. And I can’t figure out how to do that. Thanks.