isPositive
- return true
if x > 0
, otherwise false
Example: isPositive(-1)
Legal ops: !
~
&
^
|
+
<<
>>
Max ops: 8
Note: No conditional statements are allowed.
inline bool isPositive(int32_t x) {
return ???;
}
The Bitwise Xor operation treats the sign bit as it would any other bit. If one or both inputs for a pixel location are negative, the output is negative; if both inputs are positive, the output is positive.
The idea is to check whether the last bit of the number is set or not. If the last bit is set then the number is odd, otherwise even. As we know bitwise XOR Operation of the Number by 1 increment the value of the number by 1 if the number is even otherwise it decrements the value of the number by 1 if the value is odd.
int isPositive(int x) {
return !((x&(1<<31)) | !x);
}
x&(1<<31
is to check if the number is negative.
!x
is to check if the number is zero.
A number is positive if it's not negative and not zero.
return !((x & 0x80000000) >> 31 | !x);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With