从 Java/Groovy 中的固定位宽延伸的符号
Sign extending from a fixed bit width in Java/Groovy
我想在 Groovy(当然是 Java)中将 12 位数字符号扩展到 32 位。我可以在 C/C++ 等中找到各种方法,但不能找到 Groovy/Java - 有人能给我一个算法或代码片段吗?
感谢@harold:
(x << 20) >> 20 or (x ^ 0x800) - 0x800, whichever you like best/is more convenient. The first is easier to understand but it rarely gives further possibilities for simplification
我想在 Groovy(当然是 Java)中将 12 位数字符号扩展到 32 位。我可以在 C/C++ 等中找到各种方法,但不能找到 Groovy/Java - 有人能给我一个算法或代码片段吗?
感谢@harold:
(x << 20) >> 20 or (x ^ 0x800) - 0x800, whichever you like best/is more convenient. The first is easier to understand but it rarely gives further possibilities for simplification