C++ – Checking whether a number is positive or negative using bitwise operators

algorithmbit-manipulationc

I can check whether a number is odd/even using bitwise operators. Can I check whether a number is positive/zero/negative without using any conditional statements/operators like if/ternary etc.

Can the same be done using bitwise operators and some trick in C or in C++?

Best Answer

Can I check whether a number is positive/zero/negative without using any conditional statements/operators like if/ternary etc.

Of course:

bool is_positive = number > 0;
bool is_negative = number < 0;
bool is_zero = number == 0;