Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

bit-shift

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?

like image 505
jMiKon Avatar asked May 11 '12 14:05

jMiKon


People also ask

What is bit shifting used for?

Bit shifting is used when the operand is being used as a series of bits rather than as a whole. In other words, the operand is treated as individual bits that stand for something and not as a value. Bit shifting is often used in programming and has at least one variation in each programming language.

What does << mean in C?

<< is the left shift operator. It is shifting the number 1 to the left 0 bits, which is equivalent to the number 1 .

How do you calculate Left Shift and Right Shift?

@Jani A left shift corresponds by a division by 2. A right shift corresponds to multiplication my 2. This is often handy to keep in mind when doing bit shifting.

What is unsigned left shift operator in Java?

The left shift means that shift each of the bits is in binary representation toward the left. For example, when we say left shift 5 or 101 by one position. We will shift each of the bits by one position towards the left. So after shifting the number 5 towards the left by one position, the number obtained is 10 or 1010.


2 Answers

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.

like image 136
Steve McLeod Avatar answered Oct 12 '22 03:10

Steve McLeod


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

like image 44
aioobe Avatar answered Oct 12 '22 03:10

aioobe