Detection of negative integers using bit operations
- by Nawaz
One approach to check if a given integer is negative or not, could be this: (using bit operations)
int num_bits = sizeof(int) * 8; //assuming 8 bits per byte!
int sign_bit = given_int & (1 << (num_bits-1)); //sign_bit is either 1 or 0
if ( sign_bit )
{
cout << "given integer is negative"<<endl;
}
else
{
cout…