前沿拓展:整形int占多少字節(jié)

public static int byteToInt(byte b) {  
    //Java 總是把 byte 當(dāng)做有符處理;我們可以通過將其和 0xFF 進(jìn)行二進(jìn)制與得到它的無符值  
    retu病讀游rn b & 0xFF;  
}

早期的操作系統(tǒng)是16位系統(tǒng),

int 用2字節(jié)表示,范圍是-2^15 ~ 2^15-1;long 用4字節(jié)表示,范圍是-2^31 ~ 2^31-1。

后來發(fā)展到32位操作系統(tǒng),

int 用4字節(jié)表示,范圍是-2^31 ~ 2^31-1;long 與 int 相同,也用4字節(jié)表示,范圍是-2^31 ~ 2^31-1。

現(xiàn)在操作系統(tǒng)已發(fā)展到64位操作系統(tǒng),但也有所差別:

32位編譯系統(tǒng):int占4字節(jié),與long相同,范圍是-2^31 ~ 2^31-1。64位編譯系統(tǒng):int占4字節(jié),long占8字節(jié),long數(shù)據(jù)范圍變?yōu)椋?2^63~2^63-1

下圖是在64位編譯系統(tǒng)上的結(jié)果:

cppreference 給出的定義如下:

int:basic integer type. It’s guaranteed to have a width of at least 16 bits. However, on 32/64 bit systems it is almost exclusively guaranteed to have width of at least 32 bits.long:target type will have width of at least 32 bits.

long long 是 C++ 的64位整型的基本類型,從C99開始引入這個(gè)概念,在后續(xù)的標(biāo)準(zhǔn)中完善概念和定義,官方正式標(biāo)準(zhǔn)如下:

long long:target type will have width of at least 64 bits.(since C++11)

下表對(duì)不同的整數(shù)類型做了一個(gè)很好的總結(jié):

拓展知識(shí):整形int占多少字節(jié)

還有其他疑惑?想了解更多?可以點(diǎn)擊 【在線咨詢】