移位运算符如何在 Java 中工作? [英] How do shift operators work in Java?
本文介绍了移位运算符如何在 Java 中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试了解移位运算符,但没有得到太多.当我尝试执行以下代码时
I am trying to understand the shift operators and couldn't get much. When I tried to execute the below code
System.out.println(Integer.toBinaryString(2 << 11));
System.out.println(Integer.toBinaryString(2 << 22));
System.out.println(Integer.toBinaryString(2 << 33));
System.out.println(Integer.toBinaryString(2 << 44));
System.out.println(Integer.toBinaryString(2 << 55));
我得到以下
1000000000000
100000000000000000000000
100
10000000000000
1000000000000000000000000
有人可以解释一下吗?
推荐答案
System.out.println(Integer.toBinaryString(2 << 11));
将二进制 2(10
) 向左移动 11 次.因此:1000000000000
Shifts binary 2(10
) by 11 times to the left. Hence: 1000000000000
System.out.println(Integer.toBinaryString(2 << 22));
将二进制 2(10
) 向左移动 22 次.因此:100000000000000000000000
Shifts binary 2(10
) by 22 times to the left. Hence : 100000000000000000000000
System.out.println(Integer.toBinaryString(2 << 33));
现在,int 是 4 个字节,因此是 32 位.所以当你移动 33 时,它相当于移动 1.因此:100
Now, int is of 4 bytes,hence 32 bits. So when you do shift by 33, it's equivalent to shift by 1. Hence : 100
这篇关于移位运算符如何在 Java 中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文