Why does shifting an int 33 times only shift the value 1 bit?

John Zukowski

Shift operations perform an and (&) operation on the right operand before performing the shift. They reduce the operand to be less than the size (in bits) of the datatype. This is done to keep the time of the operation minimal, not letting you shift max int times to eat up CPU resources.

According to section 15.19 of the Java Language Specification, for an int, the five lowest-order bits are used, anding with 0x1f, keeping the range between 0 and 31, inclusive. For a long, the six lowest-order bits are used, anding with 0x3f, keeping the range between 0 and 63, inclusive.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact