Fastest way to get sign in Java?
I'd like to get the sign of a float value as an int value of -1 or 1. Avoiding conditionals is always a good idea in reducing computational cost. For instance, one way I can think of would be to use a fast bit-shift to get the sign: float a = ...; int sign = a >> 31; //0 for pos, 1 for neg sign = ~sign; //1 for pos, 0 for neg sign = sign << 1; //2 for pos, 0 for neg sign -= 1; //-1 for pos, 1 for neg -- perfect. Or more concisely: int sign = (~(a >> 31) << 1) - 1; Does this seem like a good approach? Will this work for all platforms, given endianness concerns (as MSB holds sign)? assylias Any