为什么右移运算符产生零而不是一?
我正在自学 Java,并完成 Thinking in Java 中的练习。
在第 116 页的练习 11 中,您应该将整数右移到其所有二进制位置,并使用 Integer.toBinaryString 显示每个位置。
public static void main(String[] args) {
int i = 8;
System.out.println(Integer.toBinaryString(i));
int maxIterations = Integer.toBinaryString(i).length();
int j;
for (j = 1; j < maxIterations; j++) {
i >>= 1;
System.out.println(Integer.toBinaryString(i));
}
在解决方案指南中,输出如下所示:
1000
1100
1110
1111
当我运行此代码时,我得到以下信息:
1000
100
10
1
这里发生了什么。数字被截断了吗?
我使用的是jdk1.6.0_20 64位。本书使用jdk1.5 32bit。
i am teaching myself java and i work through the exercises in Thinking in Java.
On page 116, exercise 11, you should right-shift an integer through all its binary positions and display each position with Integer.toBinaryString.
public static void main(String[] args) {
int i = 8;
System.out.println(Integer.toBinaryString(i));
int maxIterations = Integer.toBinaryString(i).length();
int j;
for (j = 1; j < maxIterations; j++) {
i >>= 1;
System.out.println(Integer.toBinaryString(i));
}
In the solution guide the output looks like this:
1000
1100
1110
1111
When i run this code i get this:
1000
100
10
1
What is going on here. Are the digits cut off?
I am using jdk1.6.0_20 64bit. The book uses jdk1.5 32bit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
书上好像有错误。
右移操作将所有位向右移动,删除最低有效位。如果您正确对齐结果(例如,通过用零填充),这会更有意义。
移入的最高位是:
如果您希望最终结果为 1,请尝试使用负数,例如 -8 而不是 8。
如果您使用
>>>
而不是>>
那么无论数字是正数还是负数,总是会移入零。It looks like there is an error in the book.
The right shift operation shifts all bits to the right, removing the least significant bit. This makes a lots more sense if you right align the results (by padding with zeros, for example).
The topmost bit shifted in is:
If you want the final result to be ones then try using a negative number like -8 instead of 8.
If you use
>>>
instead of>>
then a zero will always be shifted in, regardless of whether the number is positive or negative.当给定正整数作为输入时,右移运算符最终会产生零,这是正确的。
最好将其视为所有数字都向右移动、最右边的数字被截断并向左侧添加一个额外的零的操作,即模式为:
It is correct that the right shift operator eventually produces a zero when given a positive integer as input.
It's best to think of it as an operation where all digits are shifted to the right, the rightmost digit is cut off and an additional zero is added to the left, i.e. the pattern is:
从按位运算符 Java 教程页面:
由于 8 是正数,因此将移动 0。如果 i 为负数,则将移位 1(以保持整数上的符号相同)。
From the bitwise operators Java Tutorials Page:
Since 8 is positive, a zero is shifted. If
i
was negative, a one would get shifted instead (to keep the same sign on the integer).右移运算符将您的位向右移动,即
右移将使用与移位之前相同的值“填充”最左边的位。
由于最左边的位是符号,因此正值将用零填充,负值将用 1 填充。
The right shift operator moves your bits to the right, i.e.
Right shift will "fill up" the leftmost bit with the same value it had before shifting.
And since the leftmost bit is the sign, a positive value will be filled with zeros and a negative value with ones.
如果您在 int 中设置最高位,就像
您将看到所描述的行为一样,则该符号将在移位期间保留。
我认为这是一个旨在说明操作的示例。
If you set the higest bit in your int like
You will see the described behaviour, the sign will be retained during shift.
I assume it was an example intented to illustrate the operation.
您似乎认为
variable >>= 1
将 1 转移到变量上。1
实际上指定了变量移位的次数。有符号数在最高有效位位置上移动。>>>
强制数字无符号且始终按零移动。You seem to think that
variable >>= 1
shifts a one onto the variable. The1
actually specifies how many times to shift the variable. Signed numbers shift on whatever is in the most-significant bit position.>>>
forces the number to act unsigned and always shift on zeros.