5

I am trying to right shift an integer by 32 but the result is the same number. (e.g. 5 >> 32 is 5.)

If I try to do same operation on Byte and Short it works. For example, "(byte)5 >> 8" is 0.

What is wrong with Integer?

4
  • 3
    Byte is 8 bit and short is 16 bit but why they work? Sep 18, 2015 at 9:11
  • 4
    can this help? stackoverflow.com/questions/1023373/…
    – yelliver
    Sep 18, 2015 at 9:16
  • @TheLostMind Although that is true from the perspective of the JLS, shifting is not the same as a rotate which your comment seems to imply. Sep 18, 2015 at 9:37
  • 1
    @MarkRotteveel totally agree. That java wraps its shift amount is an annoying bug in the language. I.e. shifting 8 bit behaves as shifting 32 bit, but shifting 64 bit suddenly differs... wtf were they smoking? Sep 18, 2015 at 10:00

2 Answers 2

12

JLS 15.19. Shift Operators

... If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance.

so shifting 32 is not effective.

1
  • 2
    This is the real answer. The OP's code behaves that way (fundamentally) because the Java Language Specification says it must.
    – Stephen C
    Sep 18, 2015 at 9:25
1

A Shifting conversion returns result as an int or long. So, even if you shift a byte, you will get an int back.

Java code :

public static void main(String s[]) {
    byte b = 5;
    System.out.println(b >> 8);
    int i = 8;
    System.out.println(i >> 32);
}

Byte code :

         0: iconst_5
         1: istore_1
         2: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream; 
         5: iload_1
         6: bipush        8
         8: ishr
         9: invokevirtual #22      // Method java/io/PrintStream.println:(I)V  ==> Using println(int)
        12: bipush        8
        14: istore_2
        15: getstatic     #16     // Field java/lang/System.out:Ljava/io/PrintStream;
        18: iload_2
        19: bipush        32
        21: ishr
        22: invokevirtual #22      // Method java/io/PrintStream.println:(I)V   ==> Using println(int)
        25: return
8
  • @Codebender - I didn't say that it was circular Sep 18, 2015 at 9:23
  • 2
    @StephenC - I chose to un-delete this answer despite of numerous downvotes because byte-code provides valuable info here. All shifts result in either int or long. Shifting byte still results in an int. Sep 18, 2015 at 9:27
  • 3
    That is your opinion. My opinion is that the bytecodes don't prove anything. And they provide the information in an obfuscated fashion. See my comments above. This applies to just about any answer to a Java question that includes disassembled bytecodes. It is a really poor way of conveying information to human beings, and especially to novice Java programmers.
    – Stephen C
    Sep 18, 2015 at 9:31
  • 1
    @StephenC -I agree. I find it easier to check byte code and come to conclusions (which is perhaps wrong sometimes :P) instead of looking for proper jls sections Sep 18, 2015 at 9:32
  • 2
    Yes ... the JLS spec is the right place to go for a definitive answer. And if you are saying that bytecode dumps are a lazy way to answer ... I agree with that too :-)
    – Stephen C
    Sep 18, 2015 at 9:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.