(e-mail address removed) wrote, On 16/04/08 18:00:
You cannot find that by shifting since right shifting a negative value
gives an implementation defined result and left shifting a 1 in to the
MSB invokes undefined behaviour. If you want to know the sign and don't
care about negative zero then just use either <0 or >=0 as appropriate.
I think that by "MSB of a signed integer" he means
the bit which contributes the most to the magnitude
of the value rather than the sign bit.
I believe that a variation of Jack Klein's solution
for unsigned integers will also work in this case:
#include <limits.h>
#define INT_MSB_MASK (INT_MAX - (INT_MAX >> 1))
if (#include <limits.h>
#define UINT_MSB_MASK (UINT_MAX - (UINT_MAX >> 1))
if (some_unsigned_int == 0)
/* do stuff because it is not set */
else if (some_signed_int & INT_MSB_MASK)
/* do stuff because it is set */
else
/* do stuff because it is not set */