在C/C++中,只有一个右移运算符“>>”,只能用于正整数或无符号整数。在C/C++中,不建议对负数使用右移运算符,当用于负数时,输出取决于编译器。与C++不同,java支持两个右移位操作符。
null
在这里,我们将讨论列出的两个右移操作员:
- 签名右移“>>”
- 未签名的右移“>>>”
类型1:有符号右移
在Java中,运算符“>>”是带符号的右移运算符。所有整数在Java中都有符号,可以使用>>表示负数。运算符“>>”使用符号位(最左边的位)填充移位后的尾随位置。如果数字为负数,则1用作填充,如果数字为正数,则0用作填充。例如,如果一个数字的二进制表示是 1. 0…100,然后使用>>将其右移2将使 11 …….1.
例子:
JAVA
// Java Program to Illustrate Signed Right Shift Operator // Main class class GFG { // Main driver method public static void main(String args[]) { int x = - 4 ; System.out.println(x >> 1 ); int y = 4 ; System.out.println(y >> 1 ); } } |
输出
-22
类型2:无符号右移运算符
在Java中,运算符“>>>”表示无符号右移运算符,并且总是填充0,而不管数字的符号是什么。
例子:
JAVA
// Java Program to Illustrate Unsigned Right Shift Operator // Main class class GFG { // main driver method public static void main(String args[]) { // x is stored using 32 bit 2's complement form. // Binary representation of -1 is all 1s (111..1) int x = - 1 ; // The value of 'x>>>29' is 00...0111 System.out.println(x >>> 29 ); // The value of 'x>>>30' is 00...0011 System.out.println(x >>> 30 ); // The value of 'x>>>31' is 00...0001 System.out.println(x >>> 31 ); } } |
输出
731
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END