日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java移位运算_Java移位运算符 “

發布時間:2024/8/1 java 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java移位运算_Java移位运算符 “ 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

詳解

首先舉一個樣例來說明不是循環移位:

假設上面的程序改為

i = 3L << 63

程序的結果仍然為

1000000000000000000000000000000000000000000000000000000000000000

那么就說明Java中的移位運算不是循環的。

那對上面的問題又怎么解釋呢?

在JLS(Java Language Specific 15.19)中有例如以下解釋:If the promoted type of the left-hand operand is int, 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.

意思是說:在移位運算中,假設被移位的操作數是int類型的,那么僅僅會用到移位數的最低5位,假設是long類型的,那么僅僅會用到低六位。

那么為什么是低5位和低6位呢?相信你應該明確了,int共占32位,long占64位,正好是2的5次冪和6次冪。能夠理解為分別對32 和 64 取模。所以1L << 64 就會變成 1L << 0。結果自然就是1了。

關于網上的說法:

網上有很多資料說上述定義是由編譯器完畢的,即假設寫 1L << 64 。則編譯器會將文件編譯為 ?1L << 0 。可是經過本人的實驗發現這個過程會發生在執行時而不是編譯位class文件的過程。以下是個人所做的一些實驗。

實驗過程:

將程序編譯為class文件

使用javap輸出class文件的內容

使用HSDIS輸出虛擬機執行的匯編代碼

源程序:

public class SF{

public static void main(String[] args) {

new SF().sh(1,2);

}

public int sh(int a , int b){

return (a << 32);

}

}

注意此處使用的是int類型

在windows環境下的批處理文件

javac SF.java

javap -verbose SF > sfp.txt

java -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -Xcomp -XX:CompileCommand=dontinline,*SF.sh -XX:CompileCommand=compileonly,*SF.sh SF > sfasm.txt

pause

這里須要用到HSDIS插件才干輸出匯編代碼。

以下是javap的結果。

iload_1 為取得參數a,在棧中push ?32 ?后,進行移位操作。ishl中的i指代的是int的移位操作。

再看反匯編的輸出:

[Verified Entry Point]

0x01c92e50: mov %eax,-0x4000(%esp)

0x01c92e57: push %ebp

0x01c92e58: sub $0x18,%esp ;*iload_1

; - SF::sh@0 (line 7)

0x01c92e5b: shl $0x0,%edx

0x01c92e5e: mov %edx,%eax

0x01c92e60: add $0x18,%esp

0x01c92e63: pop %ebp

0x01c92e64: test %eax,0x140100 ; {poll_return}

看到在分配完棧空間后,在0x01c92e5b這一行中,進行了移位。操作數為0x0

總結

以上是生活随笔為你收集整理的java移位运算_Java移位运算符 “的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。