二進位 正數 負數 源碼反碼補碼

來源:互聯網
上載者:User
  •  二進位的正負數 .

HEX (十六進位) EA                        EA

Oct(十進位)      有符號                   無符號

                      -22                       234

Bin (二進位)     1110 1010             11101010

 

備忘:有符號數

正數 源碼,反碼,補碼都一致

負數 詳解見下,例如 :11101010

  1 1 1 0 1 0 1 0  
  符號位(不參與取反與+1行為)                
1)取反  1 0 0 1 0 1 0 1 得到反碼
2)+1 1 1 1 0 1 0 1 0 得到補碼
  1(代表負數)              

-22

 

  • java byte與16進位字串相互轉化

Hex String --> Byte

//1 Hex String --> Integer//2 Integer --> Bytepublic Byte convertFromHexString2Byte(String hexStr){Integer i = Integer.parseInt(hexStr,16);Byte b = i.byteValue();}

Byte -->Hex String

//1 Byte -- >Integer//2 Integer --> String public static String fromByte2HexString(Byte b){        Integer i = b.intValue();        String s = Integer.toHexString(i);        return s;}

 

一個byte佔用8位,一個HEX字元用4位表示 ===>1個byte 代表 2個HEX字元===>

           [] [] [] []   [] [] [] []

           高4位             低4位

           H       L

 

  • byte類型取值範圍(byte 佔8位,有符號 -128 --- 127 )

      

    推理

                  含義  
最大正數 0  1 1 1 1 1 1 1 ==>127

20 +21+….+28-1 =28 -1 =127

最大負數 1 0 0 0 0 0 0 0 ===>128

-27

  1 1 1 1 1 1 1 1 反碼  
1 1 1 1 1 1 1 1 1 補碼  
                     

 取值範圍

byte  1 位元組 8bit -2(7)---2(8)-1  有符號

-2n-1 --- 2n-1 -1

short  2位元組 16bit

 -215  -215-1

 

同上

 int 4位元組  32bit     

同上

 

 long 8位元組  64bit     

同上

 

 char 2位元組  16bit 

0--216-1

 無符號

0—2N-1

 

  • byte ==》 HEX String 轉化
public static String byte2HexString(byte[] b){           String ref = "";           for(int i=0; i<b.length;i++){              /* 
 Integer.toHexString(b[i]&0xFF);
作用是 int(十進位 有符號) ===> String(十六進位)
b[i]&0xFF 作用是 將byte轉換為int類型
byte 8 位 int 32位 byte轉int 位元不夠需要補位 補位有偏差故需要清零高24位 即&0xFF*/
String hex = Integer.toHexString(b[i]&0xFF); if(hex.length()==1){ hex='0'+hex; } ref+=hex.toUpperCase(); } return ref;}/*FF 十六進位1111 1111 二進位-1 有符號 十進位255 無符號*/
  •  與關係

兩邊都成立才可以為真

1&1 --》1

1&0 --》0

0&1--》0

0&0 --》0

 清零 ==》 &0

 置為1 == 》&1

byte b = 24;

int i = b&0xFF; //清零高24位

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.