• <small id='H8VHK'></small><noframes id='H8VHK'>

      <i id='H8VHK'><tr id='H8VHK'><dt id='H8VHK'><q id='H8VHK'><span id='H8VHK'><b id='H8VHK'><form id='H8VHK'><ins id='H8VHK'></ins><ul id='H8VHK'></ul><sub id='H8VHK'></sub></form><legend id='H8VHK'></legend><bdo id='H8VHK'><pre id='H8VHK'><center id='H8VHK'></center></pre></bdo></b><th id='H8VHK'></th></span></q></dt></tr></i><div id='H8VHK'><tfoot id='H8VHK'></tfoot><dl id='H8VHK'><fieldset id='H8VHK'></fieldset></dl></div>
        <bdo id='H8VHK'></bdo><ul id='H8VHK'></ul>
    1. <tfoot id='H8VHK'></tfoot>

      1. <legend id='H8VHK'><style id='H8VHK'><dir id='H8VHK'><q id='H8VHK'></q></dir></style></legend>

        Java是否优化了2的幂除以移位?

        Does Java optimize division by powers of two to bitshifting?(Java是否优化了2的幂除以移位?)
        <tfoot id='eSwPJ'></tfoot>

          1. <i id='eSwPJ'><tr id='eSwPJ'><dt id='eSwPJ'><q id='eSwPJ'><span id='eSwPJ'><b id='eSwPJ'><form id='eSwPJ'><ins id='eSwPJ'></ins><ul id='eSwPJ'></ul><sub id='eSwPJ'></sub></form><legend id='eSwPJ'></legend><bdo id='eSwPJ'><pre id='eSwPJ'><center id='eSwPJ'></center></pre></bdo></b><th id='eSwPJ'></th></span></q></dt></tr></i><div id='eSwPJ'><tfoot id='eSwPJ'></tfoot><dl id='eSwPJ'><fieldset id='eSwPJ'></fieldset></dl></div>
          2. <small id='eSwPJ'></small><noframes id='eSwPJ'>

                <tbody id='eSwPJ'></tbody>

              <legend id='eSwPJ'><style id='eSwPJ'><dir id='eSwPJ'><q id='eSwPJ'></q></dir></style></legend>
                <bdo id='eSwPJ'></bdo><ul id='eSwPJ'></ul>
                • 本文介绍了Java是否优化了2的幂除以移位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Java 编译器 JIT 编译器是否将除法或乘法优化为 2 的恒定幂以进行位移?

                  Does the Java compiler or the JIT compiler optimize divisions or multiplications by a constant power of two down to bitshifting?

                  例如,以下两个语句是否优化为相同?

                  For example, are the following two statements optimized to be the same?

                  int median = start + (end - start) >>> 1;
                  int median = start + (end - start) / 2;
                  

                  (基本上 这个问题但对于 Java)

                  (basically this question but for Java)

                  推荐答案

                  不,Java 编译器不这样做,因为它无法确定 (end - start) 的标志是什么 将.为什么这很重要?负整数的位移产生与普通除法不同的结果.在这里你可以看到一个演示:这个简单的测试:

                  No, the Java compiler doesn't do that, because it can't be sure on what the sign of (end - start) will be. Why does this matter? Bit shifts on negative integers yield a different result than an ordinary division. Here you can see a demo: this simple test:

                  System.out.println((-10) >> 1);  // prints -5
                  System.out.println((-11) >> 1);  // prints -6
                  System.out.println((-11) / 2);   // prints -5
                  

                  另外请注意,我使用 >> 而不是 >>>.>>> 是无符号位移,而 >> 是有符号的.

                  Also note that I used >> instead of >>>. A >>> is an unsigned bitshift, while >> is signed.

                  System.out.println((-10) >>> 1); // prints 2147483643
                  


                  @Mystical:我写了一个基准测试,表明编译器/JVM 没有进行优化:https://ideone.com/aKDShA

                  这篇关于Java是否优化了2的幂除以移位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Compiling C++ for the JVM(为 JVM 编译 C++)
                  Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                  How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                  Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                  How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                  Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)
                • <legend id='gekWB'><style id='gekWB'><dir id='gekWB'><q id='gekWB'></q></dir></style></legend>
                • <i id='gekWB'><tr id='gekWB'><dt id='gekWB'><q id='gekWB'><span id='gekWB'><b id='gekWB'><form id='gekWB'><ins id='gekWB'></ins><ul id='gekWB'></ul><sub id='gekWB'></sub></form><legend id='gekWB'></legend><bdo id='gekWB'><pre id='gekWB'><center id='gekWB'></center></pre></bdo></b><th id='gekWB'></th></span></q></dt></tr></i><div id='gekWB'><tfoot id='gekWB'></tfoot><dl id='gekWB'><fieldset id='gekWB'></fieldset></dl></div>
                    <tfoot id='gekWB'></tfoot>
                      • <bdo id='gekWB'></bdo><ul id='gekWB'></ul>

                            <small id='gekWB'></small><noframes id='gekWB'>

                              <tbody id='gekWB'></tbody>