電腦二進位總結,二進位總結

來源:互聯網
上載者:User

電腦二進位總結,二進位總結
2 進位

逢2進一的計數規則。

2進位的電腦成本最優。

原則: 電腦內部的一切都是2進位資料!

案例:

int i = 50;//i 在電腦內部就是2進位的! System.out.println(Integer.toBinaryString(i));System.out.println(i);//"50"
2進位

逢2進一的計數規則。

案例:

public static void main(String[] args) {    for(int i=0;i<=50; i++){        System.out.println(                Integer.toBinaryString(i));    }}

16進位

16進位:用於簡寫(縮寫)2進位資料。可以將每個4位2進位縮寫為一個16進位數

2進位的書寫非常繁瑣

01101001 00111111 01010101 01110101

案例:

public static void main(String[] args) {    int n = 0x693ba5e5;    System.out.println(            Integer.toBinaryString(n));     // 將2進位縮寫為16進位,並且驗證縮寫的正確性    // 01110101 11111101 10101111 01011110    // 7   5    f   d    a   f    5   e    n = 0x75fdaf5e;    System.out.println(        Integer.toBinaryString(n));    n = 50;    System.out.println(        Integer.toBinaryString(n));}
補碼

案例:

public static void main(String[] args) {    for(int i=-50; i<=50; i++){        System.out.println(            Integer.toBinaryString(i));     }}

案例:

public static void main(String[] args) {    int max = Integer.MAX_VALUE;    System.out.println(        Integer.toBinaryString(max));    int min = Integer.MIN_VALUE;    System.out.println(        Integer.toBinaryString(min));     max = 0x7fffffff;    min = 0x80000000;    int i = 0xffffffff;    System.out.println(max);    System.out.println(min);    System.out.println(i);//-1}

補碼的互補對稱現象: public static void main(String[] args) { int n = -3; System.out.println( Integer.toBinaryString(n)); System.out.println( Integer.toBinaryString(~n)); System.out.println( Integer.toBinaryString(~n+1)); int m = ~n+1; System.out.println(m);//3 }

 

int i = 0xffffffff;System.out.println(i);如上代碼的輸出結果:A.2147483647 B.-2147483648 C.2147483648 D.-1答案: Dint i = 0x80000000;System.out.println(i);如上代碼的輸出結果:A.2147483647 B.-2147483648 C.2147483648 D.-1答案: B正數溢出數負數 (錯的)正數溢出以後是隨機數(錯的)int n = 5;System.out.println(~n+1);答案:(-5)int n = 5;System.out.println(~n);答案:(-6)int n = -5;System.out.println(~n);答案:(4)int n = 0xfffffffe;    //11111111 11111111 11111111 11111110 System.out.println(~n);//00000000 00000000 00000000 00000001答案:(1)
2進位運算子
  • 取反 ~
  • 與 &
  • 或 |
  • 左移位元運算 <<
  • 數學右移位 >>
  • 邏輯右移位 >>>

位元運算的用途: 文字的編碼

字元: char 類型 16位 互連網資料: 8位

如果利用互連網傳送字元嗎必須將字元拆分為byte(8位)進行傳送

將字元拆分為位元組的拆分方案稱為字元的編碼。

最簡單的拆分方案: UTF-16BE, 將字元一分為二,無論中文還是英文都是2位元組編碼。英文浪費1個位元組,支援65535個字元。

A:  00000000 01000001 65B:  00000000 01000010 66中:01001110 00101101 20013

Unicode: 一個符號一個不重複的數,已經編碼了10萬+個符號了。

Java char類型支援編碼數量: 65535 個字元,Java 建議利用int類型支援擴充的Unicode。

UTF-8:變長編碼,英文一個位元組,中文3位元組,支援4位元組編碼,支援100萬+字元。

輸出字元的Unicode

public static void main(String[] args) {    int n = '中';    System.out.println(            Integer.toBinaryString(n)); }
與運算 & (邏輯乘法)

規則:

0 & 0 = 00 & 1 = 01 & 0 = 01 & 1 = 1

兩個數,對其位置,上下計數與

案例:

n =   00000000 00000000 01001110 00101101m =   00000000 00000000 00000000 00111111  mask  掩碼k=n&m 00000000 00000000 00000000 00101101   

代碼:

int n = 0x4e2d;int m = 0x3f;//掩碼int k = n&m;System.out.println(Integer.toBinaryString(n));System.out.println(Integer.toBinaryString(m));System.out.println(Integer.toBinaryString(k));
或運算 | (邏輯加)

規則:

0 | 0 = 00 | 1 = 11 | 0 = 11 | 1 = 1

案例:

n =     00000000 00000000 00000000 10000000m =     00000000 00000000 00000000 00101011k =n|m  00000000 00000000 00000000 10101011

代碼:

int n = 0x80;int m = 0x2b;int k = n|m;System.out.println(Integer.toBinaryString(n));System.out.println(Integer.toBinaryString(m));System.out.println(Integer.toBinaryString(k));  n =   00000000 00000000 01001110 00101101

案例: 將字元資料(Unicode)編碼為UTF-8編碼

int c = '中';int b3 = 0x80|c&0x3f;int b2 = 0x80|(c>>>6) & 0x3f;int b1 = 0xe0|(c>>>12) & 0xf;

案例: 將UTF-8編碼解碼為字元資料(Unicode)

int cc =((b1 & 0xf)<<12) |         ((b2 & 0x3f)<<6) |         (b3 & 0x3f);char ch = (char)cc;System.out.println(ch); 
移位計算的數學意義

移動小數點計算:

如: 1234278.  小數點向右移動結果 12342780. 相差10倍結果 123427800. 相差100倍如果小數點不動,則數字向左移動如:   1234278.  小數點向右移動結果  12342780. 相差10倍結果 123427800. 相差100倍

推廣: 2進位時候數字向左移動一次數字擴大2倍!

案例:

n  =      00000000 00000000 00000000 00110010  50m = n<<1  0000000 00000000 00000000 001100100  100   k = n<<2  000000 00000000 00000000 0011001000  200

代碼驗證

int n = 50;int m = n<<1;int k = n<<2;輸出 n m k 的2進位和10進位資料
最佳化計算 n*8 為 (  )答案: n<<3
區別 >>> >>

數學右移位 >> : 其結果滿足數學規律, 整除向小方向取整,負數移位,高位補1 結果還是負數。

n =      00000000 00000000 00000000 00110010  50m = n>>1 000000000 00000000 00000000 0011001  25k = n>>2 0000000000 00000000 00000000 001100  12n =      11111111 11111111 11111111 11001110  -50 m = n>>1 111111111 11111111 11111111 1100111  -25k = n>>2 1111111111 11111111 11111111 110011  -13 

邏輯右移位 >>> : 無論正負高位都補0!

n =       00000000 00000000 00000000 00110010  50m = n>>>1 000000000 00000000 00000000 0011001  25k = n>>>2 0000000000 00000000 00000000 001100  12n =       11111111 11111111 11111111 11001110  -50 m = n>>>1 011111111 11111111 11111111 1100111  k = n>>>2 0011111111 11111111 11111111 110011   

 

最佳化計算運算式 n=n+n/2 (n+=n/2) n>0 答案: n += n>>1

我是初學者,如有更新不好的,歡迎這位大神指出,謝謝大家!

更多精彩以後更新,轉載註明!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.