Check if a number is positive , negative or zero using bit operators | CPP program to find if a number is positive , negative or zero using bit wise operators . ; function to return 1 if it is zero returns 0 if it is negative returns 2 if it is positive ; string array to store all kinds of number ; function call to check the sign of number ; driver program to test the above function
#include <iostream> NEW_LINE using namespace std ; int index ( int i ) { return 1 + ( i >> 31 ) - ( - i >> 31 ) ; } void check ( int n ) { string s [ ] = { " negative " , " zero " , " positive " } ; int val = index ( n ) ; cout << n << " ▁ is ▁ " << s [ val ] << endl ; } int main ( ) { check ( 30 ) ; check ( -20 ) ; check ( 0 ) ; return 0 ; }