Hi,
I need to write a function that will return 1 if x is > 0 otherwise return 0 using only the following operations and no loops:
! ~ & ^ | + << >>
My code works for positive and negative numbers, but does not work for 0. How can I evaluate 0 as being not greater then 0?
Here is my code:
Code:int isPositive(int x) { x = x >> 31; x = x & 1; x = !x; return x; }
I'm probably missing something obvious, but no matter which way I try it, 0 always comes back as 1.
Any ideas?