Weird result of Java Integer left shift

前端 未结 2 757
慢半拍i
慢半拍i 2020-11-30 12:35

I\'m a little confused now by java left shift operation,

1<<31 =  0x80000000  --> this I can understand

But

1<         


        
2条回答
  •  时光取名叫无心
    2020-11-30 13:23

    System.out.println(Integer.toBinaryString(1 << 32)); 
    

    Shifts binary 1(10) by 32 times to the left. Hence: 1 in decimal

    System.out.println(Integer.toBinaryString(1 << 33)); 
    

    Now, int is of 4 bytes,hence 32 bits. So when you do shift by 33, it's equivalent to shift by 1. Hence : 2 in decimal

提交回复
热议问题