无符号左移?
Java没有无符号左移
无符号右移
左边补0
有符号右移
左边用原符号位补位 即正数补0效果同无符号右移、负数补1
有符号左移
右边补0
import org.junit.Test;
public class TestAaaBinary {
@Test
public void test01() {
//调用 JDK 的自带方法 toBinaryString 输出二进制数
test01(false);
//自己写的打印,结果与上面是一样的
test01(true);
}
private void test01(boolean print) {
int num_5 = 5;
int num_5_2 = -5;
System.out.println("无符号右移:正数");
for (int i = 0; i < 5; i++) {
print(num_5 >>> i, print);
}
System.out.println("无符号右移:负数");
for (int i = 0; i < 5; i++) {
print(num_5_2 >>> i, print);
}
System.out.println("有符号右移:正数");
for (int i = 0; i < 5; i++) {
print(num_5 >> i, print);
}
System.out.println("有符号右移:负数");
for (int i = 0; i < 5; i++) {
print(num_5_2 >> i, print);
}
System.out.println("有符号左移:正数");
for (int i = 0; i < 32; i++) {
print(num_5 << i, print);
}
System.out.println("有符号左移:负数");
for (int i = 0; i < 32; i++) {
print(num_5_2 << i, print);
}
}
private void print(int x, boolean print) {
if (print) {
print(x);
} else {
System.out.println(Integer.toBinaryString(x));
}
}
/**
* 输出十进制数的二进制格式
*/
private void print(int num) {
for (int i = 31; i >= 0; i--) {
System.out.print((num & (1 << i)) == 0 ? "0" : "1");
}
System.out.println();
}
}
输出如下:
无符号右移:正数
00000000000000000000000000000101
00000000000000000000000000000010
00000000000000000000000000000001
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000101
无符号右移:负数
11111111111111111111111111111011
01111111111111111111111111111101
00111111111111111111111111111110
00011111111111111111111111111111
00001111111111111111111111111111
00000111111111111111111111111111
00000011111111111111111111111111
00000001111111111111111111111111
00000000111111111111111111111111
00000000011111111111111111111111
00000000001111111111111111111111
00000000000111111111111111111111
00000000000011111111111111111111
00000000000001111111111111111111
00000000000000111111111111111111
00000000000000011111111111111111
00000000000000001111111111111111
00000000000000000111111111111111
00000000000000000011111111111111
00000000000000000001111111111111
00000000000000000000111111111111
00000000000000000000011111111111
00000000000000000000001111111111
00000000000000000000000111111111
00000000000000000000000011111111
00000000000000000000000001111111
00000000000000000000000000111111
00000000000000000000000000011111
00000000000000000000000000001111
00000000000000000000000000000111
00000000000000000000000000000011
00000000000000000000000000000001
11111111111111111111111111111011
有符号右移:正数
00000000000000000000000000000101
00000000000000000000000000000010
00000000000000000000000000000001
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000101
有符号右移:负数
11111111111111111111111111111011
11111111111111111111111111111101
11111111111111111111111111111110
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111011
有符号左移:正数
00000000000000000000000000000101
00000000000000000000000000001010
00000000000000000000000000010100
00000000000000000000000000101000
00000000000000000000000001010000
00000000000000000000000010100000
00000000000000000000000101000000
00000000000000000000001010000000
00000000000000000000010100000000
00000000000000000000101000000000
00000000000000000001010000000000
00000000000000000010100000000000
00000000000000000101000000000000
00000000000000001010000000000000
00000000000000010100000000000000
00000000000000101000000000000000
00000000000001010000000000000000
00000000000010100000000000000000
00000000000101000000000000000000
00000000001010000000000000000000
00000000010100000000000000000000
00000000101000000000000000000000
00000001010000000000000000000000
00000010100000000000000000000000
00000101000000000000000000000000
00001010000000000000000000000000
00010100000000000000000000000000
00101000000000000000000000000000
01010000000000000000000000000000
10100000000000000000000000000000
01000000000000000000000000000000
10000000000000000000000000000000
00000000000000000000000000000101
有符号左移:负数
11111111111111111111111111111011
11111111111111111111111111110110
11111111111111111111111111101100
11111111111111111111111111011000
11111111111111111111111110110000
11111111111111111111111101100000
11111111111111111111111011000000
11111111111111111111110110000000
11111111111111111111101100000000
11111111111111111111011000000000
11111111111111111110110000000000
11111111111111111101100000000000
11111111111111111011000000000000
11111111111111110110000000000000
11111111111111101100000000000000
11111111111111011000000000000000
11111111111110110000000000000000
11111111111101100000000000000000
11111111111011000000000000000000
11111111110110000000000000000000
11111111101100000000000000000000
11111111011000000000000000000000
11111110110000000000000000000000
11111101100000000000000000000000
11111011000000000000000000000000
11110110000000000000000000000000
11101100000000000000000000000000
11011000000000000000000000000000
10110000000000000000000000000000
01100000000000000000000000000000
11000000000000000000000000000000
10000000000000000000000000000000
11111111111111111111111111111011
此外,根据上面的代码,我们还可以经过测试得出
看上面的输出可能会有疑问
整型正数5
的二进制是00000000 00000000 00000000 00000101
整型负数5
的二进制是10000000 00000000 00000000 00000101
那为什么上文中对于-5
的二进制输出是11111111 11111111 11111111 11111011
呢?
先公布一下答案:这里输出的都是补码,计算机是使用补码做计算的。
下面详细解释为什么使用补码
计算机中只计算加法
5-4
转化为5+(-4)
只算加法简化了电路的设计
但是在二进制运算下,一个正数加上其相反数并不等于0,比如0000 0001 + 1000 0001 = -2
因此出现了反码,而数字本身我们叫做原码。
-5
的反码是11111111 11111111 11111111 11111010
反码存在的问题:
5 + (-5) = 0
,二进制是00000000 00000101 + 11111111 11111010 = 11111111 11111111
全1表示0(负0)0 + 0 = 0
,二进制00000000 00000000
全0也表示0(正0)还有更好的方案吗?答案就是补码
-5
的补码是11111111 11111111 11111111 11111011
上文提到过补码只有全0表示0一种情况,相比反码又简化了电路的设计
参考下面的代码:
public void test02() {
int num = 5;
System.out.println("正数5的补码是原码本身");
print(num);
System.out.println("正数5取反就是其相反数-5的反码");
print(~num);
System.out.println("-5用补码表示,是反码+1");
print(-num);
}
输出:
正数5的补码是原码本身
00000000000000000000000000000101
正数5取反就是其相反数-5的反码
11111111111111111111111111111010
-5用补码表示,是反码+1
11111111111111111111111111111011
@Test
public void test03() {
System.out.println("整型最大值是正 2的31次方-1 补码就是其本身");
print(Integer.MAX_VALUE);
System.out.println();
System.out.println("整型最小值的补码");
print(Integer.MIN_VALUE);
System.out.println("补码取反");
print(~Integer.MIN_VALUE);
System.out.println("补码取反+1 就是整型最小值的相反数 即2的31次方 因此整型最小值是负 2的31次方");
}
输出:
整型最大值是正 2的31次方-1 补码就是其本身
01111111111111111111111111111111
整型最小值的补码
10000000000000000000000000000000
补码取反
01111111111111111111111111111111
补码取反+1 就是整型最小值的相反数 即2的31次方 因此整型最小值是负 2的31次方
手机扫一扫
移动阅读更方便
你可能感兴趣的文章