例如:
l
byte c=-20;
System.out.printf( "c=%d\n",c>>-2);
byte d=123;
System.out.printf( "d=%d\n",d>>-3);
byte g=-20;
System.out.printf( "g=%d\n",g>>>-2);
byte h=-20;
System.out.printf( "h=%d\n",h>>>2);
byte e=-20;
System.out.printf( "e=%d\n",e<<-2);
byte f=20;
System.out.printf( "f=%d\n",f<<-3);
結果:
uj5u.com熱心網友回復:
將數轉為二級制然后進行移位uj5u.com熱心網友回復:
目前來說,編譯器的最大有效移位只支持 移位數字的二進制的低5位(這可能延續c/c++,也就是最大不超過31位,對于java超過31則對32取模,對于c/c++屬于未定義行為)所以
byte c=-20;
System.out.printf( "c=%d\n",c>>-2); //-2的二進制是 11111110,低5位是11110,轉十進制就是30
System.out.printf( "c=%d\n",c>>30); //所以等同于移位30
System.out.printf( "c=%d\n",c>>32); //移位超過31則對32取模,所以移32位相當于移0位(c>>(32%32))
byte d=123;
System.out.printf( "d=%d\n",d>>-3); //其它的同理,-3的二進制是11111101,低5位是11101,轉十進制就是29
System.out.printf( "d=%d\n",d>>29); //所以等同于移位30
System.out.printf( "d=%d\n",d>>32);
byte g=-20;
System.out.printf( "g=%d\n",g>>>-2);
System.out.printf( "g=%d\n",g>>>30);
System.out.printf( "g=%d\n",g>>>32);
byte h=-20;
System.out.printf( "h=%d\n",h>>>2);
byte e=-20;
System.out.printf( "e=%d\n",e<<-2);
System.out.printf( "e=%d\n",e<<30);
System.out.printf( "e=%d\n",e<<32);
byte f=20;
System.out.printf( "f=%d\n",f<<-3);
System.out.printf( "f=%d\n",f<<29);
System.out.printf( "f=%d\n",f<<32);
uj5u.com熱心網友回復:
參考java 語言規范(Java SE 8)版本的15.19章節"
15.19. Shift Operators
The operators << (left shift), >> (signed right shift), and >>> (unsigned right shift) are called the shift operators. The left-hand operand of a shift operator is the value to be shifted; the right-hand operand specifies the shift distance.
ShiftExpression:
AdditiveExpression
ShiftExpression << AdditiveExpression
ShiftExpression >> AdditiveExpression
ShiftExpression >>> AdditiveExpression
The shift operators are syntactically left-associative (they group left-to-right).
Unary numeric promotion (§5.6.1) is performed on each operand separately. (Binary numeric promotion (§5.6.2) is not performed on the operands.)
It is a compile-time error if the type of each of the operands of a shift operator, after unary numeric promotion, is not a primitive integral type.
The type of the shift expression is the promoted type of the left-hand operand.
If the promoted type of the left-hand operand is int, then only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x1f (0b11111). The shift distance actually used is therefore always in the range 0 to 31, inclusive.
If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x3f (0b111111). The shift distance actually used is therefore always in the range 0 to 63, inclusive.
At run time, shift operations are performed on the two's-complement integer representation of the value of the left operand.
The value of n << s is n left-shifted s bit positions; this is equivalent (even if overflow occurs) to multiplication by two to the power s.
The value of n >> s is n right-shifted s bit positions with sign-extension. The resulting value is floor(n / 2s). For non-negative 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, where:
If n is positive, then the result is the same as that of n >> s.
If n is negative and the type of the left-hand operand is int, then the result is equal to that of the expression (n >> s) + (2 << ~s).
If n is negative and the type of the left-hand operand is long, then the result is equal to that of the expression (n >> s) + (2L << ~s).
The added term (2 << ~s) or (2L << ~s) 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.
"
大致概括下.位移符號的兩邊的運算元通過一元型別提升后,要么是int,要么是long,否則就報錯.提升是獨立的,互不影響.不會因為右運算元提升后是long而導致左運算元也提升為long.
如果左運算元提升為int,右運算元的最低5位(bit)為位移距離,在[0,31]區間
如果左運算元提升為long,右運算元的最低6位(bit)為位移距離,在[0,63]區間
運行時,左運算元是以二進制補碼的形式執行.插一句一般資料在記憶體中都是以二進制補碼的形式存盤.
說兩個例子,比如第一個吧:-20>>-2=-1.
-20的二進制補碼為11111111111111111111111111101100,-2的二進制補碼11111111111111111111111111111110,-20是一個int,只需要低5位的bit,也就是11110,前面補零,換算成十進制也就是30,也就是11111111111111111111111111101100向右移動30位,注意此時是有符號的位移負數右移高位補1,最后為11111111111111111111111111111111,這是二進制補碼,轉為十進制是-1.
比如第三個-20>>>2=1073741819
對于-20的分析不變,補碼為11111111111111111111111111101100,2的二進制補碼0010,-20是一個int,只需要低5位的bit,也就是全部都要,轉換為十進制還是2,注意此時是無符號右移,位移程序中補0就行,11111111111111111111111111101100向右移動2位,前面補0,最后為00111111111111111111111111111011,轉換為十進制為1073741819
其它例子可以類似分析.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/139111.html
標籤:Java SE
上一篇:IDEA打開最近修改的檔案
下一篇:關于測驗如何提升測驗得深度
