Android從零開發之java-二進位

來源:互聯網
上載者:User

[java]  package test;  /*  * 作者:鐘志鋼  * 內容:二進位(原碼,反碼與補碼),位算符,移位元運算  * 時間: 2013-1-23 www.2cto.com * 1. >> 算術右移 ,符號位不變,低位溢出,並用符號位補缺少高位  *       << 算術左移 , 符號位不變,低位補零  *      >>> 邏輯右移, 低位溢出,高位補0  * 2,位元運算:~1,  按位取反 ,(0-->1, 1-->0)  *          -3^3   按位異或,一個為0, 一個為1,則為1;  *          1&3 , 按位與,兩位全為1.則結果為1;  *          1|3 , 按位或, 兩位有一個為1, 則為1;  * 3,二進位反補碼規則:對於有符號的而言;  *      1,最高位是符號符號位,0是正,1為負。  *      2,正數或0的原碼,反碼與補碼都一樣。  *      3,負數的反碼=它的原碼符號位不變,其它取反(0-->1, 1-->0)  *      4,負數的補碼=反碼 + 1;  *      5, java中沒有無符號數,即java中的數都是有符號數  *      6, 電腦運算時都是以補碼的方式運算的;  *   *   *   */  public class 二進位 {        /**      * @param args      */      public static void main(String[] args) {          二進位 er = new 二進位();        }      public 二進位 (){          //移位元運算          byte a = 1 >> 2;//低位溢出,並用符號位補缺少高位          //1 --> 00000001 -補碼-> 00000001 -移位-> 00000000.01 --> 00000000 = 0          byte aa = 8 >> 2;//低位溢出,並用符號位補缺少高位          //8 --> 00001000 -補碼-> 00001000 -移位-> 00000010.00 --> 00000010 = 2=8開平方          byte b = -1 >> 2;          //-1 --> 10000001 -補碼-> 11111111 -移位-> 11111111.11 -補碼-> 10000001 = -1          byte c = 1 << 2;//高位溢出,0補低位          //1 --> 00000001 -補碼-> 00000001 -移位-> 00000100 --> 4 = 2平方          byte d = -1 << 2;          //-1 --> 10000001 -補碼-> 11111111 -移位-> 11111100 -補碼-> 10000100 = -4          byte e = 3 >>> 2;//低位溢出,高位補0          //3--> 00000011 -邏輯移位-> 00000000,11--> 00000000=0           int ee = -3 >>> 2;//低位溢出,高位補0          //3--> 1000-24個0-0011 -->111-24個1-11101-邏輯移位-> 0011-24個1-1111,01--> 0011-24個1-1111           System.out.println("a(1 >> 2)= " + a);          System.out.println("b(-1 >> 2)= " + b);          System.out.println("c(1 << 2)= " + c);          System.out.println("d(-1 << 2)= " + d);          System.out.println("e(3 >>> 2)= " + e);          System.out.println("ee(-3 >>> 2)= " + (-3 >>> 2));          //二進位計算過程          byte f = 1;          byte g = 2;          /*f = 00000001;//byte為一個位元組,八位          *g = 00000010          *f - g --> f + (-g)          *-g = 10000010          *首先對g取反為:11111101          *再取補碼:       11111110          *f的反碼為本身00000001          *f - g =     11111111          *再取反回來:  10000000 --> 10000001          *即 f - g = -1          */                    //位元運算:          byte h = ~2;          //2 --> 00000010 -取反-> 11111101 -補到原先取反-> 10000010 -再加1-> 10000011 --> 3          byte i = 2 & 3 ;//兩個為1則為1,否則為0          //2 --> 00000010, 3 --> 00000011; 2 & 3 = 00000010 --> 2          byte j = 2 | 3;//只要有一個為1就為1,否則為0          //2 --> 00000010, 3 --> 00000011; 2 | 3 = 00000011 --> 3          byte k = ~-5;          //-5--> 10000101 -補碼-> 11111011-取反-> 00000100 -補碼-> 00000100 --> 4          byte l = 13 & 7 ;          byte m = 5 | 4;          byte n = -3 ^ 3;//不同則為1,相同則為0          //-3 --> 10000011 -補碼-> 11111101,3-->00000011,-3^3-->11111110-補碼->10000010 = -2          System.out.println("h(~2)= " +h);          System.out.println("i(2 & 3)= " +i);          System.out.println("j(2 | 3)= " +j);          System.out.println("k(~-5)= " +k);          System.out.println("l(13 & 7)= " +l);          System.out.println("m(5 | 4)= " +m);          System.out.println("n(-3 ^ 3)= " +n);      }  }   

聯繫我們

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