咖啡伴侶

          呆在上海
          posts - 163, comments - 156, trackbacks - 0, articles - 2

          JAVA 移位運算符 << >> >>>

          Posted on 2008-03-28 10:08 oathleo 閱讀(601) 評論(0)  編輯  收藏 所屬分類: Java

          /*
          * show the arthimetic character of '<<' '>>' '>>>'
          */

          public class TestArithmetic {
          ?? public TestArithmetic() {
          ?? }
          ??
          ?? public?? static void?? main(String [] args){
          ???? int minus = -10;
          ???? System.out.println(" Binary of -10 is " + Integer.toBinaryString(minus));
          ???? System.out.println(" Arthimetic minus by -10 << 2 = " + (minus<<2) + " Binary is " + Integer.toBinaryString(minus<<2));
          ???? System.out.println(" Arthimetic minus by -10 >> 2 = " + (minus>>2) + " Binary is " + Integer.toBinaryString(minus>>2));
          ???? System.out.println(" Arthimetic minus by -10 >>>2 =?? " + (minus >>> 2) + " Binary is " + Integer.toBinaryString(minus>>>2)
          ??????????????????????? + ",length is " + Integer.toBinaryString(minus>>>2).length());
          ????
          ???? int plus = 10;
          ???? System.out.println(" Binary of 10 is " + Integer.toBinaryString(plus));
          ???? System.out.println(" Arthimetic minus by 10 << 2 = " + (plus<<2)+ "Binary is " + Integer.toBinaryString(plus<<2));
          ???? System.out.println(" Arthimetic minus by 10 >> 2 = " + (plus>>2)+ "Binary is "+ Integer.toBinaryString(plus>>2));
          ???? System.out.println(" Arthimetic minus by 10 >>>2 =?? " + (plus >>> 2)+ "Binary is "+ Integer.toBinaryString(plus >>> 2));
          ?? }

          補充知識:數值的補碼表示也分兩種情況:
          (1)正數的補碼:與原碼相同。
          例如,+9的補碼是00001001。
          (2)負數的補碼:符號位為1,其余位為該數絕對值的原碼按位取反;然后整個數加1。
          例如,-7的補碼:因為是負數,則符號位為“1”,整個為10000111;其余7位為-7的絕對值+7的原碼0000111按位取反為1111000;再加1,所以-7的補碼是11111001。


          已知一個數的補碼,求原碼的操作分兩種情況:
          (1)如果補碼的符號位為“0”,表示是一個正數,所以補碼就是該數的原碼。
          (2)如果補碼的符號位為“1”,表示是一個負數,求原碼的操作可以是:符號位為1,其余各位取反,然后再整個數加1。
          例如,已知一個補碼為11111001,則原碼是10000111(-7):因為符號位為“1”,表示是一個負數,所以該位不變,仍為“1”;其余7位1111001取反后為0000110;再加1,所以是10000111。

          主站蜘蛛池模板: 怀柔区| 黎川县| 华池县| 砚山县| 隆昌县| 玉龙| 郯城县| 苍梧县| 高唐县| 江源县| 兰州市| 柞水县| 黎川县| 永嘉县| 西乡县| 涟源市| 秦皇岛市| 张家口市| 泸州市| 土默特右旗| 福州市| 崇文区| 枞阳县| 白沙| 阳春市| 合阳县| 大石桥市| 莱阳市| 银川市| 定兴县| 五原县| 于都县| 阜阳市| 山东省| 韶关市| 屏南县| 东明县| 虎林市| 门头沟区| 中宁县| 德安县|