在编程中,按位移位运算符>>
表示算术右移 , >>>
表示逻辑右移 ,其差为:
-
>>
,它在右移n位(扩展符号)后保留符号(正数或负数)。 -
>>>
,它会在右移n位(零扩展)后忽略符号。
要使用按位移位运算符>>
和>>>
。 首先,我们需要知道Java如何使用二进制补码来表示有符号数(正数和负数)。
1. Java和Two的补语。
Java使用二进制补码来表示带符号的数字(正数和负数)。 在下面的示例中,我们将向您展示如何用二进制补码表示正数和负数。
注意
在二进制补码中,左第一位(最高有效位)代表符号,0是正数,1是负数
1.1例如2和-2
2的二进制格式是0000 0010
0000 0010
-2(二进制补码)的二进制格式是1111 1110
0000 0010 (positive 2)
1111 1101 (invert bits)
1111 1110 (add 1)
1111 1110 (-2 in binary)
1.2另一个示例19和-19
19的二进制格式是0001 0011
0001 0011
-19的二进制格式是1110 1101
。
0001 0011 (positive 19)
1110 1100 (invert bits)
1110 1101 (add 1)
1110 1101 (-19 in binary)
2.算术右移>>或带符号的扩展名。
算术右移>>
或带符号的右移保留位移位后的符号(正数或负数)。 此>>
运算符使用原始的最高有效位(左侧的第一位)填充所有左侧的空白位置。
![算术右移](https://i-blog.csdnimg.cn/blog_migrate/95d36f1b8cfe66270efd63715faba56c.png)
PS照片从Wikipedia复制。
2.1例如,以8位为数字19,然后算术右移2位19 >> 2
。
19 = |0001 0011|
>> 2
19 >> 2 = |??00 0100|11
在上面的示例中,最后2位11
从8位中移出了,但是我们应该在左边的前两个空白位置填充什么呢?
答案取决于原始的最高有效位(msb)。 在此示例中,原始msb为0,我们填写??
与0。
19 = |{0=msb}001 0011|
>> 2
19 >> 2 = |??00 0100|11
19 >> 2 = |0000 0100|11
2.2另一个例子,用8位数字-19,我们算术右移2位-19 >> 2
。
-19 = |1110 1101|
>> 2
-19 >> 2 = |??11 1011|01
在上面的示例中,原始msb为1,我们填充了??
与11
。
-19 = |{1=msb}110 1101|
>> 2
-19 >> 2 = |??11 1011|01
-19 >> 2 = |1111 1011|01
同样,以二进制补码形式,左边的第一位(msb)表示符号,0是正数,1是负数。 复制原始最高有效位(msb)以填充空位的概念将在移位后保留符号-有符号扩展名 。
题
问:等等, -19 >> 2
的答案是什么?
答:答案是1111 1011
或-5
。
这就是二进制补数对负数起作用的方式。
1111 1011 (this is a negative number)
1111 1010 (sub 1)
0000 0101 (invert bits)
0000 0101 (1x2^0 + 1x2^2 = 5)
在二进制补码中,左边的第一位为1表示否定,答案为-5。
3.逻辑右移>>>或零扩展。
此>>>
逻辑右移忽略该符号,并用零填充所有左边的空白位置。
![逻辑右移](https://i-blog.csdnimg.cn/blog_migrate/4a7ac8b76029a9bd6084a6397c84a987.png)
PS照片从Wikipedia复制。
3.1例如,以8位为数字19,我们将逻辑右移2位19 >>> 2
。
19 = |0001 0011|
>>> 2
19 >>> 2 = |??00 0100|11
19 = |0001 0011|
>>> 2
19 >>> 2 = |0000 0100|11
3.2对于负数-19,它是相同的。 无论什么条件,逻辑右移都将用零填充左空白位置,而忽略符号。
-19 = |1110 1101|
>>> 2
-19 >>> 2 = |??11 1011|01
-19 = |1110 1101|
>>> 2
-19 >>> 2 = |0011 1011|01
题
问:再等等, -19 >>> 2
的答案是什么?
对于8位。 答案是59。
0011 1011 (positive number)
0011 1011 (1x2^0) + (1x2^1) + (1x2^3) + (1x2^4) + (1x2^5)
0011 1011 (1 + 2 + 8 + 16 + 32) = 59
在Java中,整数是32位4字节。 答案是1073741819。
System.out.println(-19>>>2); // 1073741819
在Java中, -19
,将其转换为二进制。
Input -19 = 11111111|11111111|11111111|11101101
-19 >> 2 = 11111111|11111111|11111111|11111011 = -5
-19 >>> 2 = 00111111|11111111|11111111|11111011 = 1073741819
这些按位运算符>>>或>>有什么用例? 请发表评论,谢谢。
注意
如果您发现任何错误或错字,特别是二进制格式,请发表评论并让我知道🙂
4.二进制格式的Java Print Integer。
这是Java程序,使用Integer.toBinaryString
以Binary格式打印Integer
。
package com.mkyong.crypto.bytes;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ByteSign {
public static final String OUTPUT_FORMAT = "%32s";
public static void main(String[] args) {
String STRING_FORMAT = "%-10s = %s";
int number = 24;
System.out.println("Positive Number");
System.out.println(String.format(STRING_FORMAT, "Input " + number, printBinary(number)));
System.out.println(String.format(STRING_FORMAT, number + " >> 2", printBinary(number >> 2)));
System.out.println(String.format(STRING_FORMAT, number + " >>> 2", printBinary(number >>> 2)));
int number2 = -19;
System.out.println("\nNegative Number");
System.out.println(String.format(STRING_FORMAT, "Input " + number2, printBinary(number2)));
System.out.println(String.format(STRING_FORMAT, number2 + " >> 2", printBinary(number2 >> 2)));
System.out.println(String.format(STRING_FORMAT, number2 + " >>> 2", printBinary(number2 >>> 2)));
}
public static String printBinary(int number) {
return printBinary(number, 8, "|");
}
public static String printBinary(int number, int blockSize, String separator) {
// pad leading zero
String pBinary = String
.format(OUTPUT_FORMAT, Integer.toBinaryString(number))
.replace(" ", "0");
// split by blockSize
List<String> result = new ArrayList<>();
int index = 0;
while (index < pBinary.length()) {
result.add(pBinary.substring(index, Math.min(index + blockSize, pBinary.length())));
index += blockSize;
}
return result.stream().collect(Collectors.joining(separator));
}
}
输出量
Positive Number
Input 24 = 00000000|00000000|00000000|00011000
24 >> 2 = 00000000|00000000|00000000|00000110
24 >>> 2 = 00000000|00000000|00000000|00000110
Negative Number
Input -19 = 11111111|11111111|11111111|11101101
-19 >> 2 = 11111111|11111111|11111111|11111011
-19 >>> 2 = 00111111|11111111|11111111|11111011
参考文献
翻译自: https://mkyong.com/java/java-and-bitwise-shift-operator/