# 徹底弄懂Java的移位操作符

## 左移操作符 <<

``````public static void main(String[] args) {
int i = -1;
System.out.println("Before << , i's value is " + i);
System.out.println("i's binary string is " + Integer.toBinaryString(i));
i <<= 10;
System.out.println("After << , i's value is " + i);
System.out.println("i's binary string is " + Integer.toBinaryString(i));
}
``````

Java的`int`佔32位，因此對`i = -1`轉換成二進制數，然後左移10位，其結果是左邊高10位丟棄，右邊低10位補`0`，再轉換為十進制，得到`i = -1024`的結果。

``````Before << , i's value is -1
i's binary string is 11111111111111111111111111111111
After << , i's value is -1024
i's binary string is 11111111111111111111110000000000
``````

## 帶符號右移操作符 >>

``````public static void main(String[] args) {
// 對正數進行右移操作
int i1 = 4992;
System.out.println("Before >> , i1's value is " + i1);
System.out.println("i1's binary string is " + Integer.toBinaryString(i1));
i1 >>= 10;
System.out.println("After >> , i1's value is " + i1);
System.out.println("i1's binary string is " + Integer.toBinaryString(i1));
// 對負數進行右移操作
int i2 = -4992;
System.out.println("Before >> , i2's value is " + i2);
System.out.println("i2's binary string is " + Integer.toBinaryString(i2));
i2 >>= 10;
System.out.println("After >> , i2's value is " + i2);
System.out.println("i2's binary string is " + Integer.toBinaryString(i2));
}
``````

``````Before >> , i1's value is 4992
i1's binary string is 1001110000000
After >> , i1's value is 4
i1's binary string is 100
Before >> , i2's value is -4992
i2's binary string is 11111111111111111110110010000000
After >> , i2's value is -5
i2's binary string is 11111111111111111111111111111011
``````

## 無符號右移操作符 >>>

``````public static void main(String[] args) {
int i3 = -4992;
System.out.println("Before >>> , i3's value is " + i3);
System.out.println("i3's binary string is " + Integer.toBinaryString(i3));
i3 >>>= 10;
System.out.println("After >>> , i3's value is " + i3);
System.out.println("i3's binary string is " + Integer.toBinaryString(i3));
}
``````

``````Before >>> , i3's value is -4992
i3's binary string is 11111111111111111110110010000000
After >>> , i3's value is 4194299
i3's binary string is 1111111111111111111011
``````

## 真的懂了嗎？

### 對 short、byte、char 的移位操作

``````public static void main(String[] args) {
byte b = -1;
System.out.println("Before >> , b's value is " + b);
System.out.println("b's binary string is " + Integer.toBinaryString(b));
b >>>= 6;
System.out.println("After >> , b's value is " + b);
System.out.println("b's binary string is " + Integer.toBinaryString(b));
}
``````

Java的`byte`佔8位，按照前面講述的原理，對`b = -1`轉換為二進制數後，右移6位，左邊高6位補`0`，右邊低位丟棄，其結果應該是`b = 3`

``````Before >> , b's value is -1
b's binary string is 11111111111111111111111111111111
After >> , b's value is -1
b's binary string is 11111111111111111111111111111111
``````

``````public static void main(String[] args) {
byte b = -1;
System.out.println("Before >> , b's value is " + b);
System.out.println("b's binary string is " + Integer.toBinaryString(b));
System.out.println("After >> , b's value is " + (b >>> 6));
System.out.println("b's binary string is " + Integer.toBinaryString(b >>> 6));
}
``````

``````Before >> , b's value is -1
b's binary string is 11111111111111111111111111111111
After >> , b's value is 67108863
b's binary string is 11111111111111111111111111
``````

### 如果移位的位數超過數值所佔有的位數會怎樣？

``````public static void main(String[] args) {
int i4 = -1;
System.out.println("Before >>> , i4's value is " + i4);
System.out.println("i4's binary string is " + Integer.toBinaryString(i4));
System.out.println("After >>> 31 , i4's value is " + (i4 >>> 31));
System.out.println("i4's binary string is " + Integer.toBinaryString(i4 >>> 31));
System.out.println("After >>> 32 , i4's value is " + (i4 >>> 32));
System.out.println("i4's binary string is " + Integer.toBinaryString(i4 >>> 32));
System.out.println("After >>> 33 , i4's value is " + (i4 >>> 33));
System.out.println("i4's binary string is " + Integer.toBinaryString(i4 >>> 33));
}
``````

NO！Java對移位操作符的右操作數`rhs`有特別的處理，對於`int`類型，只取其低5位，也就是取`rhs % 32`的結果；對於long類型，只取其低6位，也即是取`rhs % 64`的結果。因此，對於`i4 >>> 32`，實際上是`i4 >>> (32 % 32)`，也即`i4 >>> 0`，結果仍然是`-1`

``````Before >>> , i4's value is -1
i4's binary string is 11111111111111111111111111111111
After >>> 31 , i4's value is 1
i4's binary string is 1
After >>> 32 , i4's value is -1
i4's binary string is 11111111111111111111111111111111
After >>> 33 , i4's value is 2147483647
i4's binary string is 1111111111111111111111111111111
``````

## 總結

### 相關文章

Promises/A+實現，一條規範對應一段代碼

JS類型

js循環語句中的async與await