Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of left shifting zero by any amount?

Tags:

java

Upon reading the ASM 4.1 source code I've found instances of the following:

int ASM4 = 4 << 16 | 0 << 8 | 0; int ASM5 = 5 << 16 | 0 << 8 | 0; 

Does these left shifts of zero by 8 do anything to the expression, or the 'or' by 0 for that matter?

Wouldn't it be equivalent to just have:

int ASM4 = 4 << 16; int ASM5 = 5 << 16; 
like image 710
0-0 Avatar asked Feb 16 '15 18:02

0-0


People also ask

What is zero fill left shift?

The left-shift operator causes the bits in shift-expression to be shifted to the left by the number of positions specified by additive-expression . The bit positions that have been vacated by the shift operation are zero-filled.

What is left shift used for?

The left shift operator ( << ) shifts the first operand the specified number of bits, modulo 32, to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right.

What does a left shift do to a number?

The number to the left of the operator is shifted the number of places specified by the number to the right. Each shift to the left doubles the number, therefore each left shift multiplies the original number by 2. Use the left shift for fast multiplication or to pack a group of numbers together into one larger number.

What is the left shift of 0 by 1?

1 in binary is 0001 , then bitshifting it by 0 won't do anything, which aligns with what you observed. So any number x << 0 is equivalent to x * 2^0 , which is x * 1 , which is just x .


2 Answers

Indeed they are equivalent but one possible explanation is that they wanted to map the version numbers including both the major and minor numbers to a unique ID in their code. So in the following:

int ASM4 = 4 << 16 | 0 << 8 | 0; // this looks like 4.0.0 int ASM5 = 5 << 16 | 0 << 8 | 0; // this looks list 5.0.0 

The 4 and 5 represent versions 4 and 5 respectively, and the zero in 0 << 8 could potentially be the minor numbers, and the last zero is another minor number, as in 4.0.0 and 5.0.0. But that's my guess anyway. You'd really have to ask the authors.

like image 110
M A Avatar answered Sep 21 '22 17:09

M A


In context:

// ASM API versions  int ASM4 = 4 << 16 | 0 << 8 | 0; int ASM5 = 5 << 16 | 0 << 8 | 0; 

Yes, this is equivalent to

int ASM4 = 4 << 16; int ASM5 = 5 << 16; 

This is just written that way to make it clear that we are setting the 3rd byte to 4, and both lower bytes to 0. Alternatively, that it is a version number that should be read as 4.0.0.

like image 35
Adrian Leonhard Avatar answered Sep 25 '22 17:09

Adrian Leonhard