<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 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 閱讀(405) 評論(0)  編輯  收藏 所屬分類: Java學習
    主站蜘蛛池模板: 青青青青青青久久久免费观看 | 免费观看又污又黄在线观看| 亚洲av无码专区国产乱码在线观看 | 成人毛片免费观看视频大全| 久久精品免费一区二区三区| 亚洲爆乳大丰满无码专区| 亚洲精品无码不卡| 亚洲精品第一国产综合精品99 | 激情内射亚洲一区二区三区爱妻| 亚洲国产精品无码久久久蜜芽| 日韩a在线观看免费观看| www视频在线观看免费| 国产拍拍拍无码视频免费| 欧洲乱码伦视频免费国产| 亚洲国产精品美女久久久久| 亚洲人成电影网站| 亚洲专区在线视频| 亚洲AV日韩AV天堂一区二区三区 | 老司机精品视频免费| 亚洲欧洲无码AV不卡在线| 亚洲AV无码久久久久网站蜜桃 | 三年片在线观看免费西瓜视频| 日韩a毛片免费观看| 亚洲码和欧洲码一码二码三码 | 日韩免费a级毛片无码a∨| 18女人腿打开无遮掩免费| 久久国产免费一区二区三区| 在线涩涩免费观看国产精品| 韩国免费A级毛片久久| 9i9精品国产免费久久| 日本红怡院亚洲红怡院最新| 中文字幕在亚洲第一在线| 免费一级毛片不卡不收费| 免费国产美女爽到喷出水来视频| 日本无卡码免费一区二区三区| 精品久久洲久久久久护士免费| 成年女人色毛片免费看| 性做久久久久免费看| 国产99视频精品免费视频7| 免费a级毛片无码av| 国产亚洲欧洲Aⅴ综合一区 |