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

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章檔案

          搜索

          •  

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

          移位運(yùn)算符面向的運(yùn)算對象也是二進(jìn)制的“位”。可單獨(dú)用它們處理整數(shù)類型(主類型的一種)。左移位運(yùn)算符(<<)能將運(yùn)算符左邊的運(yùn)算對象向左移動(dòng)運(yùn)算符右側(cè)指定的位數(shù)(在低位補(bǔ)0)。“有符號”右移位運(yùn)算符(>>)則將運(yùn)算符左邊的運(yùn)算對象向右移動(dòng)運(yùn)算符右側(cè)指定的位數(shù)。“有符號”右移位運(yùn)算符使用了“符號擴(kuò)展”:若值為正,則在高位插入0;若值為負(fù),則在高位插入1。Java也添加了一種“無符號”右移位運(yùn)算符(>>>),它使用了“零擴(kuò)展”:無論正負(fù),都在高位插入0。這一運(yùn)算符是C或C++沒有的。
            若對char,byte或者short進(jìn)行移位處理,那么在移位進(jìn)行之前,它們會自動(dòng)轉(zhuǎn)換成一個(gè)int。只有右側(cè)的5個(gè)低位才會用到。這樣可防止我們在一個(gè)int數(shù)里移動(dòng)不切實(shí)際的位數(shù)。若對一個(gè)long值進(jìn)行處理,最后得到的結(jié)果也是long。此時(shí)只會用到右側(cè)的6個(gè)低位,防止移動(dòng)超過long值里現(xiàn)成的位數(shù)。但在進(jìn)行“無符號”右移位時(shí),也可能遇到一個(gè)問題。若對byte或short值進(jìn)行右移位運(yùn)算,得到的可能不是正確的結(jié)果(Java 1.0和Java 1.1特別突出)。它們會自動(dòng)轉(zhuǎn)換成int類型,并進(jìn)行右移位。但“零擴(kuò)展”不會發(fā)生,所以在那些情況下會得到-1的結(jié)果。可用下面這個(gè)例子檢測自己的實(shí)現(xiàn)方案:
            //: 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);
             }
            } ///:~
            移位可與等號(<<=或>>=或>>>=)組合使用。此時(shí),運(yùn)算符左邊的值會移動(dòng)由右邊的值指定的位數(shù),再將得到的結(jié)果賦回左邊的值。
            下面這個(gè)例子向大家闡示了如何應(yīng)用涉及“按位”操作的所有運(yùn)算符,以及它們的效果:
            //: 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();
             }
            } ///:~
            程序末尾調(diào)用了兩個(gè)方法:pBinInt()和pBinLong()。它們分別操作一個(gè)int和long值,并用一種二進(jìn)制格式輸出,同時(shí)附有簡要的說明文字。目前,可暫時(shí)忽略它們具體的實(shí)現(xiàn)方案。
            大家要注意的是System.out.print()的使用,而不是System.out.println()。print()方法不會產(chǎn)生一個(gè)新行,以便在同一行里羅列多種信息。
            除展示所有按位運(yùn)算符針對int和long的效果之外,本例也展示了int和long的最小值、最大值、+1和-1值,使大家能體會它們的情況。注意高位代表正負(fù)號:0為正,1為負(fù)。下面列出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
            數(shù)字的二進(jìn)制形式表現(xiàn)為“有符號2的補(bǔ)值”。

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

          posted on 2007-09-12 11:05 teasp 閱讀(407) 評論(0)  編輯  收藏 所屬分類: Java學(xué)習(xí)
          主站蜘蛛池模板: 万山特区| 鹿泉市| 梧州市| 衡南县| 石泉县| 绍兴市| 桃江县| 旬邑县| 西乡县| 武定县| 银川市| 三亚市| 观塘区| 大厂| 白水县| 申扎县| 新乡县| 苏尼特右旗| 东光县| 宝鸡市| 绥芬河市| 冷水江市| 井冈山市| 湾仔区| 峡江县| 隆林| 普陀区| 毕节市| 阿拉善盟| 北海市| 古浪县| 南京市| 县级市| 涞源县| 项城市| 勐海县| 南丹县| 赤壁市| 新化县| 巩义市| 临江市|