Java-按位运算符
在本教程中,我们将学习Java编程语言中的按位运算符。
我们使用按位运算符来处理位,即0和1。
以下是我们可以在Java中使用的按位运算符。
| 运算符 | 说明 |
|---|---|
| & | 按位AND |
| | | 按位OR |
| ^ | 按位异或者,即XOR |
| ~ | 取反 |
| << | 向左移 |
| >> | 向右移 |
| >>> | 以零填充向右移动 |
按位运算符可能不适用于float和double。
按位与
仅当两个操作数均为1时,按位AND运算符&才会给出1,否则为0。
按位与的真值表。
| A | B | A & B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
在下面的示例中,我们有两个整数值1和2,我们将执行按位与运算并显示结果。
class Bitwise {
public static void main(String args[]) {
//declare variables
int x = 1;
int y = 2;
System.out.println("Result: " + x + " & " + y + " = " + (x & y));
}
}
Result: 1 & 2 = 0
上面代码的按位与运算的计算。
//representing decimal in 32 bits (4 bytes) 1 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0001 (binary) & 2 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0010 (binary) --------------------------------------------------------------- 0 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0000 (binary)
单击此处了解如何将数字从十进制转换为二进制。
按位或者
仅当两个操作数均为0时,按位OR|才会为0,否则为1。
按位或者的真值表。
| A | B | A | B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
在下面的示例中,我们将执行按位或者运算,并使用两个整数值1和2显示结果。
class Bitwise {
public static void main(String args[]) {
//declare variables
int x = 1;
int y = 2;
System.out.println("Result: " + x + " | " + y + " = " + (x | y));
}
}
Result: 1 | 2 = 3
上面代码的按位或者运算的计算。
//representing decimal in 32 bits (4 bytes) 1 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0001 (binary) | 2 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0010 (binary) --------------------------------------------------------------- 3 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0011 (binary)
按位异或者
按位XOR^将为1的奇数给出1,否则为0。
按位XOR的真值表。
| A | B | A ^ B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
在下面的示例中,我们有两个整数值2和7,我们将执行按位XOR运算并显示结果。
class Bitwise {
public static void main(String args[]) {
//declare variables
int x = 2;
int y = 7;
System.out.println("Result: " + x + " ^ " + y + " = " + (x ^ y));
}
}
Result: 2 ^ 7 = 5
上面代码的按位XOR操作的计算。
//representing decimal in 32 bits (4 bytes) 2 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0010 (binary) ^ 7 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0111 (binary) --------------------------------------------------------------- 5 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0101 (binary)
取反
补码~~是一元运算符,它将0转换为1,并将1转换为0。
在下面的示例中,我们使用2的整数,并将计算其补数。
class Bitwise {
public static void main(String args[]) {
//declare variables
int x = 2;
System.out.println("Result: ~" + x + " = " + (~x));
}
}
Result: ~2 = -3
以上代码的计算。
//representing decimal in 32 bits (4 bytes)
2 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0010 (binary)
~2 = 1111 1111 1111 1111 1111 1111 1111 1101 (binary)
= -3 (decimal)
我们得到-3,因为我们正在处理带符号的整数。
注意!数据类型" int"的范围为-2,147,483,648到2,147,483,647。
单击此处获取Java数据类型教程。
左移
我们使用左移" <<"运算符将位左移。
在下面的示例中,我们有一个整数,我们将向左移1位。
class Bitwise {
public static void main(String args[]) {
//declare variables
int x = 4;
System.out.println("Result: " + x + " << 1 = " + (x << 1));
}
}
Result: 4 << 1 = 8
计算方式:
//representing decimal in 32 bits (4 bytes)
4 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0100 (binary)
-------------------------------------------------------------
4 << 1 = 0000 0000 0000 0000 0000 0000 0000 1000 (binary)
= 8 (decimal)
右移
我们使用右移" >>"运算符将位右移。
在下面的示例中,我们有一个整数,我们将右移1个位置。
class Bitwise {
public static void main(String args[]) {
//declare variables
int x = 4;
System.out.println("Result: " + x + " >> 1 = " + (x >> 1));
}
}
Result: 4 >> 1 = 2
计算方式:
//representing decimal in 32 bits (4 bytes)
4 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0100 (binary)
-------------------------------------------------------------
4 >> 1 = 0000 0000 0000 0000 0000 0000 0000 0010 (binary)
= 2 (decimal)
零填充右移
我们使用零填充运算符">"来向右移位,以向右移位位,并用零填充左侧。
在下面的示例中,我们有一个整数,我们将用零填充1的位置右移。
class Bitwise {
public static void main(String args[]) {
//declare variables
int x = -1;
System.out.println("Result: " + x + " >>> 1 = " + (x >>> 1));
}
}
Result: -1 >>> 1 = 2147483647
计算方式:
//representing decimal in 32 bits (4 bytes)
-1 (decimal) = 1111 1111 1111 1111 1111 1111 1111 1111 (binary)
--------------------------------------------------------------
-1 >>> 1 = 0111 1111 1111 1111 1111 1111 1111 1111 (binary)
= 2147483647 (decimal)
右移>>和零填充>>>右移之间的区别
当使用正数进行运算时,两个运算符都将最左边的位用0填充。
但是差异是由负数引起的。
对于负数,最左边的位是1。
因此,当我们使用右移运算符" >>"时,最左边的位将保留为1。
而当我们将右移与零填充运算符" >>>"一起使用时,则将最左边但设置为0,从而将负数转换为正数。

