I am working on a function, sign, that returns 1 if a given int is positive, 0 if zero and -1 if negative. I am only allowed to use the following operations: ! ~ & ^ | + << >>. So far, I have tried an array of things, and have gotten extremely close but don't know how to distinguish between a 1 and -1.
int sign(int x) {
return (((x >> 31) & 1) ^ (!x));
}
Am I missing something here? I am pulling my hair out trying to figure this out.