[英]Why does System.out.println(-1<<32)=-1 in Java?
Why does System.out.println(-1<<32)
display -1
in Java? 为什么System.out.println(-1<<32)
在Java中显示-1
? Is there any root cause? 有根本原因吗? I hope somebody can explain! 我希望有人能解释一下!
In Java (and JavaScript), a << b
automatically wraps b
modulo the bit length of the numeric type. 在Java(和JavaScript)中, a << b
自动包装b
模数数字类型的位长。 You're doing it with an int
, which is 32 bits, and 32 % 32 = 0, therefore, no change. 你是用int
做的,它是32位,32%32 = 0,所以没有变化。 You can always just check for a b
of more than (bit length - 1) and automatically set to zero instead of shifting. 你总是可以检查一个超过(位长 - 1)的b
并自动设置为零而不是移位。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.