What's the purpose to bit-shift int value by zero?

橙三吉。 提交于 2019-12-01 15:12:52

问题


Looking to the source code of java.nio.DirectByteBuffer class, I have found this:

if ((length << 0) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) ....

What is the purpose to shift length by zero bits? May this be some perfomance optimization or something else?


回答1:


I think I've solved it.

In the class JavaDocs:

// -- This file was mechanically generated: Do not edit! -- //

So it is not hand coded. It was script-generated and the script writer did not add an optimisation for the case when the amount to bit shift by is zero.




回答2:


Doing i << 0 is a no-op. It evaluates to the same as i.




回答3:


The i << 0 is plainly redundant. There is no good reason for a Java programmer to write this code deliberately.

I'd say that this code is:

  • written by someone who wasn't thinking,
  • written by someone who doesn't understand what the << operator does,
  • the result of some semi-mechanical refactoring, or
  • originally produced by some sort of code generator or translator.

However, there's a good chance that the bytecode or JIT compiler will optimize this away, or that it won't impact significantly on performance anyway.



来源:https://stackoverflow.com/questions/10553212/whats-the-purpose-to-bit-shift-int-value-by-zero

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!