bit twiddling得到32位int的符号位

时间:2015-06-19 18:30:20

标签: bit-manipulation

我想转换

(n < 0 ? 1 : 0)

陷入困境(假设2s补充拱)。

出于性能原因。

1 个答案:

答案 0 :(得分:2)

使用无符号移位

x = n >>> 31; // Java's unsigned shift

x = (int)((uint)n >> 31); // C#'s unsigned shift, the casts are effectively nop

GCC自动执行此操作,其他编译器也可以。或不。您的里程可能会有所不同。