JAVA运营商项目怎么样,Java的>>对比>>>运营商?

本文详细介绍了Java中算术右移(>>)和无符号右移(>>>)的区别。算术右移在移位时保留符号位,而无符号右移则用零填充。对于正数,两者行为相同;但对于负数,算术右移会保持其负号,无符号右移则会变成正数。此外,文章还通过实例展示了这两种运算符的不同效果,并解释了在某些CPU上如何通过组合运算实现算术移位。
摘要由CSDN通过智能技术生成

本问题已经有最佳答案,请猛点这里访问。

我没有Java参考书,我很难找到谷歌的答案。

Java中的">>"和">>>"运算符有什么区别?

int value = 0x0100;

int result = (value >> 8);

System.out.println("(value >> 8) =" + result);  // Prints:"(value >> 8) = 1"

result = (value >>> 8);

System.out.println("(value >>> 8) =" + result); // Prints:"(value >>> 8) = 1"

可能重复:>>>和>>之间的差异,>>>和>>运算符之间的差异

有符号整数使用高位来表示符号。

所以>>会保留符号,而>>>则不会。这就是>>被称为算术移位而>>>被称为逻辑移位的原因。

这样,你可以做(??假设32位整数)以下内容:

-10 >> 1得到-5(0xFFFFFFF6 >> 1产生0xFFFFFFFB - 注意高阶位保持不变。)

-10 >>> 1产生2147483643(0xFFFFFFF6 >>> 1产生0x7FFFFFFB - 注意所有位都被移位,因此高位位现在为零。根据二进制补码arithemetic,该数字不再为负数。)

对于正整数,>>和>>>的作用相同,因为高位已经为零。

它还解释了为什么不需要<<

+1最有用的答案

@BillK虽然答案很好,但是当你要发表评论时,你还没有看到,所以你要求你不要写像+1这样的东西或者谢谢?

@PriydarshiSingh我通常不会,但我认为有必要指出这应该在接受的答案之上投票(似乎已经奏效)。 如果我只说"最有用的答案"会更好吗? 另外,你绝对肯定这个指南存在于09年吗?

>>>是逻辑移位,>>是算术移位。

来自Java Notes:按位运算符:

n >> p(右移)

移动n个右p位的位。如果n是2的补码有符号数,则符号位移入高位。

示例:5 >> 2 = 1

n >>> p(右移)

移动n个右p位的位。零被转移到高阶位置。

示例:-4 >>> 28 = 15

对于正数,没有区别。负数(二进制补码)数字将用>>填充零和>>。

1010 0110 >>> 2 = 0010 1001

1010 0110 >> 2 = 1110 1001

正确答案已多次发布,但不是来自权威来源。

这来自JLS§15.19班次操作员:

The shift operators include left shift <>, and unsigned right shift >>>; they are syntactically left-associative (they group left-to-right). The left-hand operand of a shift operator is the value to be shifted; the right-hand operand specifies the shift distance.

...

The value of n>>s is n right-shifted s bit positions with sign-extension. The resulting value is ⌊n/2s⌋. For nonnegative values of n, this is equivalent to truncating integer division, as computed by the integer division operator /, by two to the power s.

The value of n>>>s is n right-shifted s bit positions with zero-extension. If n is positive, then the result is the same as that of n>>s; if n is negative, the result is equal to that of the expression (n>>s)+(2< if the type of the left-hand operand is int, and to the result of the expression (n>>s)+(2L< if the type of the left-hand operand is long. The added term (2< or (2L< cancels out the propagated sign bit. (Note that, because of the implicit masking of the right-hand operand of a shift operator, ~s as a shift distance is equivalent to 31-s when shifting an int value and to 63-s when shifting a long value.)

非常感谢您的回复。 它回答了我遇到的另一个问题:如何在仅具有逻辑移位的CPU上有效地执行算术移位。"(n >> s)+(2 <

>>是一个算术移位,它保留任何"空"位中的符号位。另一个是逻辑转变,用零填充空白点。

对于有符号整数,算术移位>>除以2,而对于无符号数,逻辑移位>>>除以2(如果将有符号Java int中的位模式解释为无符号整数)。

它与有符号值数学有关。对于高阶位,>>>以零为单位,>>保留符号位并将其拉入。

一些信息

the >> operator preserves the leftmost bits. The leftmost bits are filled with the previous content. This is to do with sign extension. In this case there is a 1 at the left and it is preserved. If you do not want to keep the 1 to the left, use the >>> operator which shifts 0's into the leftmost bits

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值