>>>之间的差异和>>运营商 [英] Difference between >>> and >> operators

查看:114
本文介绍了>>>之间的差异和>>运营商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果移位的数字为正>> >>且>>工作相同。

If the shifted number is positive >>> and >> work the same.

如果移位的数字为负>>>用1s填充最高有效位而>>操作将MSB填充为0。

If the shifted number is negative >>> fills the most significant bits with 1s whereas >> operation shifts filling the MSBs with 0.

我的理解是否正确?

如果负数存储时MSB设置为1,而不是Java使用的2s补码方式,运算符的行为完全不同,正确吗?

If the negative numbers are stored with the MSB set to 1 and not the 2s complement way that Java uses the the operators would behave entirely differently, correct?

推荐答案

负数表示的方式称为2的补码。为了演示这是如何工作的,以-12为例。 12,二进制,是00001100(假设整数是8位,但实际上它们要大得多)。通过简单地反转每一位得到2的补码,你得到11110011.然后,只需加1得到11110100.注意,如果你再次应用相同的步骤,你会得到正12回。

The way negative numbers are represented is called 2's complement. To demonstrate how this works, take -12 as an example. 12, in binary, is 00001100 (assume integers are 8 bits though in reality they are much bigger). Take the 2's complement by simply inverting every bit, and you get 11110011. Then, simply add 1 to get 11110100. Notice that if you apply the same steps again, you get positive 12 back.

>>>无论如何都会变为零,所以12 >>> 1应该给你00000110,这是6,而(-12)>>> 1应该给你01111010,即122。你实际上在Java中尝试这个,你会得到一个更大的数字,因为Java整数实际上远大于8位。

The >>> shifts in zero no matter what, so 12 >>> 1 should give you 00000110, which is 6, and (-12) >>> 1 should give you 01111010, which is 122. If you actually try this in Java, you'll get a much bigger number since Java ints are actually much bigger than 8 bits.

>>转换有点相同最高位,正数保持正数,负数保持负数。 12 >> 1是00000110(仍然是6)和( - 12)>> 1将是11111010,这是负6。

The >> shifts in a bit identical to the highest bit, so that positive numbers stay positive and negative numbers stay negative. 12 >> 1 is 00000110 (still 6) and (-12) >> 1 would be 11111010 which is negative 6.

这篇关于>>>之间的差异和>>运营商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆