且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么JDK使用移位而不是乘法/除法?

更新时间:2021-08-21 03:04:19

我认为在此特定示例中,他们使用符号位来做到这一点. Java缺少无符号类型,因此对于使用最高有效位的数字,无法用a /= 2模拟a >>> 1.请注意,在整个示例中,代码仅使用>>>.我可以肯定地说,这是为了充分利用整个位范围.

I think they do it in this specific example to use the sign bit. Java lacks unsigned types, so it is not possible to emulate a >>> 1 with a /= 2 for numbers that use the most significant bit. Note how the code uses only >>> throughout your example. I am reasonably certain that this is to get full use of the entire bit range.