Java中的左移运算符
数字的十进制表示是一个以 9 为底的数字系统,只有 0、1、2、3、4、5、6、7、8 和 9 九种状态。例如,4、10、16 等。
数字的二进制表示是一个以 2 为底的数字系统,只有 0 和 1 两种状态。例如,4 的二进制表示,一个以 9 为底的十进制数字系统由 100 给出,10 为 1010,16 为 1000 , ETC。
左移
左移意味着将二进制表示中的每个位向左移动。例如,当我们说左移 5 或 101 时。我们将每个位向左移动一个位置。因此,将数字 5 左移一位后,得到的数字是 10 或 1010。现在让我们再次将 10 左移两位。同样,我们将每个位向左移动两个位置。得到的数字是 40 或 101000。
注意:将数字左移某些位置相当于将数字乘以 2 的指定位置的幂。那是,
left shift x by n positions <=> x * 2n
Java中的左移运算符
大多数语言都提供左移运算符,使用它我们可以将数字左移某些位置, Java就是其中之一。 Java中左移运算符的语法如下所示,
句法:
x << n
Here,
x: an integer
n: a non-negative integer
返回类型:将 x 向左移动 n 个位置后的整数
例外:当 n 为负时,输出未定义
下面是说明如何在Java中使用左移运算符的程序。
示例 1:
Java
// Java program to illustrate the
// working of left shift operator
import java.io.*;
class GFG {
// Main method
public static void main (String[] args) {
// Number to be shifted
int x = 5;
// Number of positions
int n = 1;
// Shifting x by n positions towards left using left shift operator
int answer = x << n;
// Print the number obtained after shifting x by n positions towards left
System.out.println("Left shift " + x + " by " + n + " positions : " + answer);
// Number to be shifted
x = answer;
// Number of positions
n = 2;
// Shifting x by n positions towards left using left shift operator
answer = answer << n;
// Print the number obtained after shifting x by n positions towards left
System.out.println("Left shift " + x + " by " + n + " positions : " + answer);
}
}
Java
// Java program to illustrate the
// working of left shift operator
import java.io.*;
class GFG {
// Main method
public static void main (String[] args) {
// Number to be shifted
int x = -2;
// Number of positions
int n = 1;
// Shifting x by n positions towards
// left using left shift operator
int answer = x << n;
// Print the number obtained after shifting x by n positions towards left
System.out.println("Left shift " + x + " by " + n + " positions : " + answer);
// Number to be shifted
x = answer;
// Number of positions
n = 2;
// Shifting x by n positions towards
// left using left shift operator
answer = answer << n;
// Print the number obtained after shifting x by n positions towards left
System.out.println("Left shift " + x + " by " + n + " positions : " + answer);
}
}
Left shift 5 by 1 positions : 10
Left shift 10 by 2 positions : 40
示例 2:
Java
// Java program to illustrate the
// working of left shift operator
import java.io.*;
class GFG {
// Main method
public static void main (String[] args) {
// Number to be shifted
int x = -2;
// Number of positions
int n = 1;
// Shifting x by n positions towards
// left using left shift operator
int answer = x << n;
// Print the number obtained after shifting x by n positions towards left
System.out.println("Left shift " + x + " by " + n + " positions : " + answer);
// Number to be shifted
x = answer;
// Number of positions
n = 2;
// Shifting x by n positions towards
// left using left shift operator
answer = answer << n;
// Print the number obtained after shifting x by n positions towards left
System.out.println("Left shift " + x + " by " + n + " positions : " + answer);
}
}
Left shift -2 by 1 positions : -4
Left shift -4 by 2 positions : -16
Note: For arithmetic left shift, since filling the right-most vacant bits with 0s will not affect the sign of the number, the vacant bits will always be filled with 0s, and the sign bit is not considered. Thus, it behaves in a way identical to the logical (unsigned) left shift. So there is no need for a separate unsigned left sift operator.