隨筆 - 11  文章 - 33  trackbacks - 0
          <2007年9月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章檔案

          搜索

          •  

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

          移位運算符面向的運算對象也是二進制的“位”。可單獨用它們處理整數類型(主類型的一種)。左移位運算符(<<)能將運算符左邊的運算對象向左移動運算符右側指定的位數(在低位補0)。“有符號”右移位運算符(>>)則將運算符左邊的運算對象向右移動運算符右側指定的位數。“有符號”右移位運算符使用了“符號擴展”:若值為正,則在高位插入0;若值為負,則在高位插入1。Java也添加了一種“無符號”右移位運算符(>>>),它使用了“零擴展”:無論正負,都在高位插入0。這一運算符是C或C++沒有的。
            若對char,byte或者short進行移位處理,那么在移位進行之前,它們會自動轉換成一個int。只有右側的5個低位才會用到。這樣可防止我們在一個int數里移動不切實際的位數。若對一個long值進行處理,最后得到的結果也是long。此時只會用到右側的6個低位,防止移動超過long值里現成的位數。但在進行“無符號”右移位時,也可能遇到一個問題。若對byte或short值進行右移位運算,得到的可能不是正確的結果(Java 1.0和Java 1.1特別突出)。它們會自動轉換成int類型,并進行右移位。但“零擴展”不會發生,所以在那些情況下會得到-1的結果。可用下面這個例子檢測自己的實現方案:
            //: URShift.java
            // Test of unsigned right shift
            public class URShift {
             public static void main(String[] args) {
              int i = -1;
              i >>>= 10;
              System.out.println(i);
              long l = -1;
              l >>>= 10;
              System.out.println(l);
              short s = -1;
              s >>>= 10;
              System.out.println(s);
              byte b = -1;
              b >>>= 10;
              System.out.println(b);
             }
            } ///:~
            移位可與等號(<<=或>>=或>>>=)組合使用。此時,運算符左邊的值會移動由右邊的值指定的位數,再將得到的結果賦回左邊的值。
            下面這個例子向大家闡示了如何應用涉及“按位”操作的所有運算符,以及它們的效果:
            //: BitManipulation.java
            // Using the bitwise operators
            import java.util.*;
            public class BitManipulation {
             public static void main(String[] args) {
              Random rand = new Random();
              int i = rand.nextInt();
              int j = rand.nextInt();
              pBinInt("-1", -1);
              pBinInt("+1", +1);
              int maXPos = 2147483647;
              pBinInt("maxpos", maxpos);
              int maxneg = -2147483648;
              pBinInt("maxneg", maxneg);
              pBinInt("i", i);
              pBinInt("~i", ~i);
              pBinInt("-i", -i);
              pBinInt("j", j);
              pBinInt("i & j", i & j);
              pBinInt("i j", i j);
              pBinInt("i ^ j", i ^ j);
              pBinInt("i << 5", i << 5);
              pBinInt("i >> 5", i >> 5);
              pBinInt("(~i) >> 5", (~i) >> 5);
              pBinInt("i >>> 5", i >>> 5);
              pBinInt("(~i) >>> 5", (~i) >>> 5);
              long l = rand.nextLong();
              long m = rand.nextLong();
              pBinLong("-1L", -1L);
              pBinLong("+1L", +1L);
              long ll = 9223372036854775807L;
              pBinLong("maxpos", ll);
              long lln = -9223372036854775808L;
              pBinLong("maxneg", lln);
              pBinLong("l", l);
              pBinLong("~l", ~l);
              pBinLong("-l", -l);
              pBinLong("m", m);
              pBinLong("l & m", l & m);
              pBinLong("l m", l m);
              pBinLong("l ^ m", l ^ m);
              pBinLong("l << 5", l << 5);
              pBinLong("l >> 5", l >> 5);
              pBinLong("(~l) >> 5", (~l) >> 5);
              pBinLong("l >>> 5", l >>> 5);
              pBinLong("(~l) >>> 5", (~l) >>> 5);
             }
             static void pBinInt(String s, int i) {
              System.out.println(
               s + ", int: " + i + ", binary: ");
              System.out.print("  ");
              for(int j = 31; j >=0; j--)
               if(((1 << j) & i) != 0)
                System.out.print("1");
               else
                System.out.print("0");
              System.out.println();
             }
             static void pBinLong(String s, long l) {
              System.out.println(
               s + ", long: " + l + ", binary: ");
              System.out.print("  ");
              for(int i = 63; i >=0; i--)
               if(((1L << i) & l) != 0)
                System.out.print("1");
               else
                System.out.print("0");
              System.out.println();
             }
            } ///:~
            程序末尾調用了兩個方法:pBinInt()和pBinLong()。它們分別操作一個int和long值,并用一種二進制格式輸出,同時附有簡要的說明文字。目前,可暫時忽略它們具體的實現方案。
            大家要注意的是System.out.print()的使用,而不是System.out.println()。print()方法不會產生一個新行,以便在同一行里羅列多種信息。
            除展示所有按位運算符針對int和long的效果之外,本例也展示了int和long的最小值、最大值、+1和-1值,使大家能體會它們的情況。注意高位代表正負號:0為正,1為負。下面列出int部分的輸出:
            -1, int: -1, binary:
              11111111111111111111111111111111
            +1, int: 1, binary:
              00000000000000000000000000000001
            maxpos, int: 2147483647, binary:
              01111111111111111111111111111111
            maxneg, int: -2147483648, binary:
              10000000000000000000000000000000
            i, int: 59081716, binary:
              00000011100001011000001111110100
            ~i, int: -59081717, binary:
              11111100011110100111110000001011
            -i, int: -59081716, binary:
              11111100011110100111110000001100
            j, int: 198850956, binary:
              00001011110110100011100110001100
            i & j, int: 58720644, binary:
              00000011100000000000000110000100
            i j, int: 199212028, binary:
              00001011110111111011101111111100
            i ^ j, int: 140491384, binary:
              00001000010111111011101001111000
            i << 5, int: 1890614912, binary:
              01110000101100000111111010000000
            i >> 5, int: 1846303, binary:
              00000000000111000010110000011111
            (~i) >> 5, int: -1846304, binary:
              11111111111000111101001111100000
            i >>> 5, int: 1846303, binary:
              00000000000111000010110000011111
            (~i) >>> 5, int: 132371424, binary:
              00000111111000111101001111100000
            數字的二進制形式表現為“有符號2的補值”。

          (出處:http://www.vipcn.com

          posted on 2007-09-12 11:05 teasp 閱讀(407) 評論(0)  編輯  收藏 所屬分類: Java學習
          主站蜘蛛池模板: 昌宁县| 图片| 宝应县| 大厂| 无为县| 红河县| 广西| 伊通| 江油市| 郧西县| 台安县| 榆社县| 台湾省| 永吉县| 定结县| 湘乡市| 宣武区| 南漳县| 贡觉县| 嘉定区| 体育| 通州市| 临湘市| 嘉义县| 芮城县| 东台市| 休宁县| 邵阳市| 台前县| 册亨县| 资中县| 阆中市| 沅江市| 黄冈市| 建德市| 交城县| 西昌市| 泗洪县| 观塘区| 怀化市| 乌审旗|