2013-05-18 22 views
0

1字節= 8位java.a中的字節範圍是-128到127如何有可能?

我將1111 1111的二進制數轉換爲十進制數,它給了我255

但是,當我將0111 1111 binaru數字轉換爲十進制數,它給我127

那麼在什麼基礎範圍宣佈。請幫助我。

在此先感謝...

+1

不確定是什麼問題。 127 + 2 ** 7 = 255。 –

+1

字節在java.And中被標記(包括正數和負數)並且從-128到127. –

+2

它是-128到+127,而不是-127到+128。 – Haozhun

回答

4

Java中的數字類型的簽字,這意味着它們可以是消極或積極的。最左邊的位(most significant bit)用於表示符號,其中1表示負數,0表示正數。

字節

Max 01111111 = +127 
Min 10000000 = -128 

    11111111 = -1 

Max 0111111111111111 = +32767 
Min 1000000000000000 = -32768 

    0000000011111111 = +255 

二進制負數是以2's complement form表示。

+0

如果最左邊的位是符號,那麼10000000的值是0? – PSR

+0

incase of negative number,left most bit is 1 which which two work is tell that number is negative and also calculated as 128. see my answer answer below –

+0

'10000000 = -128'因爲補碼 –

1

一位保留用於判斷數字是否定或正數。

因此,對於最大陽性數值將是

01111111 which gives the int number as 128(leftmost bit 0 represent its a postive number) 

     64+32+6+8+4+2+1= 127 

爲最大negativenumber值將((最左邊的位1表示它的負數))

10000000 which gives the int number as -128 

    -128+0+0+0+0+0+0+0 = -128 

所以範圍從

變得
-127 to 128 
相關問題