27
Check whether a number x is nonzero using the legal operators except !.
Examples: isNonZero(3) = 1, isNonZero(0) = 0
Legal ops: ~ & ^ | + << >>
- Note : Only bitwise operators should be used.
if,else,for, etc. cannot be used. - Edit1 : No. of operators should not exceed 10.
- Edit2 : Consider size of
intto be 4 bytes.
int isNonZero(int x) {
return ???;
}
Using ! this would be trivial , but how do we do it without using ! ?