按位移位运算符。签名和未签名 [英] Bitwise shift operators. Signed and unsigned
问题描述
我正在使用互联网上的补习说进行SCJP考试。
I'm practising for the SCJP exam using cram notes from the Internet.
根据我的说明,>>
运算符应该是右移符号,符号位从左边进来。虽然左移运算符<<
应该保留符号位。
According to my notes the >>
operator is supposed to be signed right shift, with the sign bit being brought in from the left. While the left shift operator <<
is supposed to preserve the sign bit.
然后玩,我能够使用<<
运算符移动符号(fe Integer.MAX_VALUE<< 1
评估为 -2
,而我永远无法使用>>
运算符来移动符号。
Playing around however, I'm able to shift the sign with the <<
operator (f.e. Integer.MAX_VALUE << 1
evaluates to -2
, while I'm never able to shift the sign with the >>
operator.
我必须在这里误解一些东西,但是什么?
I must be misunderstanding something here, but what?
推荐答案
>>签名是因为它保留了符号。它使用数字的二进制表示中最左边的数字作为填充。例如:
">>" is signed because it keeps the sign. It uses the most left digit in binary representation of a number as a filler. For example:
| this value is used as a filler
11011011
>> 11101101
01010010
>> 00101001
>>>是此运算符的无符号版本。它总是使用零作为填充符:
">>>" is unsigned version of this operator. It always use zero as a filler:
11011011
>>> 01101101
01010010
>>> 00101001
在二进制表示中,最左边的数字确定数字的符号。所以,如果它是'1'那么我们有负值,如果它是'0' - 那么我们的数字是正数。这就是为什么使用最左边的数字作为填充符号可以使符号保持永久性。
In binary representation the most left digit determines sign of the number. So, if it's '1' then we have negative value and if it's '0' - then our number is positive. That's why using the most left digit as a filler allows to keep sign permanent.
这篇关于按位移位运算符。签名和未签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!