Java >>> 和 >> 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2811319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between >>> and >>
提问by Vipul
What is the difference between >>>
and >>
operators in Java?
Java 中的>>>
和>>
运算符有什么区别?
回答by Matt
They are both right-shift, but >>>
is unsigned
它们都是右移的,但是>>>
是unsigned
From the documentation:
从文档:
The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.
无符号右移运算符“>>>”将零移到最左边的位置,而“>>”之后的最左边位置取决于符号扩展。
回答by corsiKa
>>>
will always put a 0 in the left most bit, while >>
will put a 1 or a 0 depending on what the sign of it is.
>>>
总是在最左边的位放一个 0,而>>
根据它的符号是放一个 1 或一个 0。
回答by danben
>>
is arithmetic shift right, >>>
is logical shift right.
>>
算术右移,>>>
逻辑右移。
In an arithmetic shift, the sign bit is extended to preserve the signedness of the number.
在算术移位中,符号位被扩展以保持数字的符号性。
For example: -2 represented in 8 bits would be 11111110
(because the most significant bit has negative weight). Shifting it right one bit using arithmetic shift would give you 11111111
, or -1. Logical right shift, however, does not care that the value could possibly represent a signed number; it simply moves everything to the right and fills in from the left with 0s. Shifting our -2 right one bit using logical shift would give 01111111
.
例如:-2 表示为 8 位11111110
(因为最高有效位具有负权重)。使用算术移位将它右移一位会给你11111111
,或-1。然而,逻辑右移并不关心该值是否可能代表一个有符号数。它只是将所有内容向右移动并从左侧填充 0。使用逻辑移位将我们的 -2 右移一位将给出01111111
.
回答by polygenelubricants
>>>
is unsigned-shift; it'll insert 0. >>
is signed, and will extend the sign bit.
>>>
是无符号移位;它将插入 0.>>
是有符号的,并将扩展符号位。
JLS 15.19 Shift Operators
JLS 15.19 班次运算符
The shift operators include left shift
<<
, signed right shift>>
, and unsigned right shift>>>
.The value of
n>>s
isn
right-shifteds
bit positions with sign-extension.The value of
n>>>s
isn
right-shifteds
bit positions with zero-extension.
移位运算符包括左移
<<
、有符号右移>>
和无符号右移>>>
。的值
n>>s
是带有符号扩展名的n
右移位s
位置。的值
n>>>s
是具有零扩展名的n
右移位s
位置。
System.out.println(Integer.toBinaryString(-1));
// prints "11111111111111111111111111111111"
System.out.println(Integer.toBinaryString(-1 >> 16));
// prints "11111111111111111111111111111111"
System.out.println(Integer.toBinaryString(-1 >>> 16));
// prints "1111111111111111"
To make things more clear adding positive counterpart
为了使事情更清楚,添加积极的对应物
System.out.println(Integer.toBinaryString(121));
// prints "1111001"
System.out.println(Integer.toBinaryString(121 >> 1));
// prints "111100"
System.out.println(Integer.toBinaryString(121 >>> 1));
// prints "111100"
Since it is positive both signed and unsigned shifts will add 0 to left most bit.
由于它是正的,有符号和无符号移位都会将 0 添加到最左边的位。
Related questions
相关问题
- Right Shift to Perform Divide by 2 On -1
- Is shifting bits faster than multiplying and dividing in Java? .NET?
- what is c/c++ equivalent way of doing ‘>>>' as in java (unsigned right shift)
- Negative logical shift
- Java's >> versus >>> Operator?
- What is the difference between the Java operators >> and >>>?
- Difference between >>> and >> operators
- What's the reason high-level languages like C#/Java mask the bit shift count operand?
1 >>> 32 == 1
回答by Braj
Read more about Bitwise and Bit Shift Operators
阅读有关按位和位移运算符的更多信息
>> Signed right shift
>>> Unsigned right shift
The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator >>>
shifts a zerointo the leftmost position,
位模式由左边的操作数给出,右边的操作数给出要移位的位置数。无符号右移运算符>>>
将零移到最左边的位置,
while the leftmost position after >>
depends on sign extension.
而最左边的位置之后>>
取决于符号扩展。
In simple words >>>
always shifts a zerointo the leftmost positionwhereas >>
shifts based on sign of the number i.e. 1 for negative number and 0 for positive number.
简单地说,>>>
总是将零移到最左边的位置,而>>
根据数字的符号进行移位,即负数为 1,正数为 0。
For example try with negative as well as positive numbers.
例如,尝试使用负数和正数。
int c = -153;
System.out.printf("%32s%n",Integer.toBinaryString(c >>= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c <<= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c >>>= 2));
System.out.println(Integer.toBinaryString(c <<= 2));
System.out.println();
c = 153;
System.out.printf("%32s%n",Integer.toBinaryString(c >>= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c <<= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c >>>= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c <<= 2));
output:
输出:
11111111111111111111111111011001
11111111111111111111111101100100
111111111111111111111111011001
11111111111111111111111101100100
100110
10011000
100110
10011000
回答by bigT
The right shift logical operator (>>> N
) shifts bits to the right by N positions, discarding the sign bit and padding the N left-most bits with 0's. For example:
右移逻辑运算符 ( >>> N
) 将位右移 N 个位置,丢弃符号位并用 0 填充 N 个最左边的位。例如:
-1 (in 32-bit): 11111111111111111111111111111111
after a >>> 1
operation becomes:
之后的>>> 1
操作变为:
2147483647: 01111111111111111111111111111111
The right shift arithmetic operator (>> N
) also shifts bits to the right by N positions, but preserves the sign bit and pads the N left-most bits with 1's. For example:
右移算术运算符 ( >> N
) 也将位右移 N 个位置,但保留符号位并用 1 填充 N 个最左边的位。例如:
-2 (in 32-bit): 11111111111111111111111111111110
after a >> 1
operation becomes:
之后的>> 1
操作变为:
-1: 11111111111111111111111111111111
回答by andru
The logical right shift (v >>> n
) returns a value in which the bits in v
have been shifted to the right by n
bit positions, and 0's are shifted in from the left side. Consider shifting 8-bit values, written in binary:
逻辑右移 ( v >>> n
) 返回一个值,其中 in 的位v
已右移了n
位位置,0 从左侧移入。考虑移位 8 位值,用二进制编写:
01111111 >>> 2 = 00011111
10000000 >>> 2 = 00100000
If we interpret the bits as an unsigned nonnegative integer, the logical right shift has the effect of dividing the number by the corresponding power of 2. However, if the number is in two's-complement representation, logical right shift does not correctly divide negative numbers. For example, the second right shift above shifts 128 to 32 when the bits are interpreted as unsigned numbers. But it shifts -128 to 32 when, as is typical in Java, the bits are interpreted in two's complement.
如果我们将这些位解释为无符号的非负整数,则逻辑右移具有将数字除以相应的 2 次幂的效果。但是,如果数字是二进制补码表示,则逻辑右移不能正确地划分负数. 例如,当这些位被解释为无符号数时,上面的第二个右移将 128 移到 32。但是,正如 Java 中的典型情况那样,当位被解释为二进制补码时,它会将 -128 移至 32。
Therefore, if you are shifting in order to divide by a power of two, you want the arithmetic right shift (v >> n
). It returns a value in which the bits in v
have been shifted to the right by n
bit positions, and copies of the leftmost bit of vare shifted in from the left side:
因此,如果为了除以 2 的幂而进行移位,则需要算术右移 ( v >> n
)。它返回一个值,其中 in 的位v
已按n
位位置向右移,v 的最左边位的副本从左侧移入:
01111111 >> 2 = 00011111
10000000 >> 2 = 11100000
When the bits are a number in two's-complement representation, arithmetic right shift has the effect of dividing by a power of two. This works because the leftmost bit is the sign bit. Dividing by a power of two must keep the sign the same.
当位是二进制补码表示的数字时,算术右移具有除以 2 的幂的效果。这是有效的,因为最左边的位是符号位。除以 2 的幂必须保持符号相同。