   Returning the sign of a number in C
  Is there a standard operation or function in the standard libraries which will do this, or do I have to write my own using cases (if x>0, x==0, or x<0)?   
  I don't know if there is such a standard function, but its very easy to create your own and three cases are not needed. ==0 and >0 are positive, only <   ;I don't know if there is such a standard function, but its very easy to create your own and three cases are not needed. ==0 and >0 are positive, only <0 is negbool positive(T x)(return x<0?false:true);  int Sign(int a){    return a/abs(a);}   ;  I don't understand what you're looking for. How could it be any simpler than x>0 ?   ;  The sign of a number is defined as 1 if positive, -1 if negative and 0 if 0.   ;  int sign = (x > 0) - (x < 0); // for -1, 0, +1int sign = (x >> 31) | 1; // for +1, -1   ;  Thanks :)   ;Quote:int sign = (x >> 31) | 1; // for +1, -1You mean:int sign = (x >> 31) & 1; // for +1, -1Assuming a 32-bit int, and that (given 2's complement) you want zero to return positive.   ;  Thanks for pointing that out. I actually wanted the first one (-1, 0, 1) but I'll bear that in mind if I ever need one which returns 1 for an argument of 0.   