Ete*_*ner 16 c bit-manipulation
isPositive-返回true如果x > 0,否则false
例: isPositive(-1)
法律行动: ! ~ & ^ | + << >>
Max ops: 8
注意:不允许使用条件语句.
inline bool isPositive(int32_t x) {
return ???;
}
Run Code Online (Sandbox Code Playgroud)
cod*_*ict 19
int isPositive(int x) {
return !((x&(1<<31)) | !x);
}
Run Code Online (Sandbox Code Playgroud)
x&(1<<31 是检查数字是否为负数.
!x 是检查数字是否为零.
如果数字不是负数而不是零,则数字为正数.
Mat*_*hew 11
return !((x & 0x80000000) >> 31 | !x);
Run Code Online (Sandbox Code Playgroud)
Hen*_*rik 10
int isPositive(int x)
{
return (!(x & 0x80000000) & !!x);
}
Run Code Online (Sandbox Code Playgroud)