C Bit Twiddling
int value=-25; int sign = 0; //First Method sign = -(value < 0); //Second method sign = 1 | value >> (sizeof(int)*8 - 1); //Third Method sign = (value>0) - (value<0); printf("%d",sign);
The statement sizeof(int) returns the value 4 which means that the integer variable requires 4 bytes of storage.
The operation 1 | -1 gives us the value of -1
page revision: 1, last edited: 20 Apr 2008 15:57