JAVA位、賦值、條件運算子等

來源:互聯網
上載者:User

標籤:賦值   .com   http   技術分享   優先   log   priority   stat   system   

public class NewClass {    public void 位元運算符(){        /*         * 1 ~ 按位取反 1=~0         * 2 & 按位與   0&1=0  1&1=1  0&0=0         * 3 | 按位或   0|1=1  1|1=1  0|0=0         * 4 ^ 按位異或 0^1=1  1^1=0  0^0=0  1^0=1   相同得0,相異得1         */          int x=53;         int r=~x;         System.out.println("r="+r);    //-54,按位取反加一         int y=22;   //    10110         System.out.println("x&y="+(x&y));    //20         System.out.println("x|y="+(x|y));  //55         System.out.println("x^y="+(x^y));    //35                  /*         * 移位元運算符         * << 算數左移運算子  每向左移動一位,右邊用0補齊,左邊多出的位元丟掉         * >> 算數右移運算子  每向右移動一位,左邊用0補齊,右邊多出的位元丟掉         * >>>邏輯右移運算子         */         System.out.println("4<<2="+(4<<2));  // 0000 0100(4)左移兩位為0001 0000(16)         System.out.println("4<<3="+(4<<3));  //32         System.out.println("12>>2="+(12>>2));  //0000 1100(12)右移兩位為0000 0011(3)         System.out.println("-12>>2="+(-12>>2)); //-3         System.out.println("12>>>2="+(12>>>2));         System.out.println("-12>>>2="+(-12>>>2));                  /*         *位元運算優先順序別:  ~  <<  >>  >>>  &  ^         */    }        public void priorityOfBitOperator(){        int a=-67,b=116,c=78;        //a=10111101,b=01110100,c=01001110        int d=~a|b&c;        int e=~c>>2&(a|~b);        int f=a^b&c;        System.out.println("d="+d);        System.out.println("e="+e);        System.out.println("f"+f);    }                public void 賦值運算子(){            //普通賦值運算子            byte b=123;            int i=b;            int a=222;            int j=i+a;            //擴充賦值運算子            //  +=  -=  *=  /=  %=  &=  |=  ^=  >>=  <<=  >>>=            i+=1;  // i=i+1   124            i+=100;  //  i=i+100   224            i-=10;   //  i=i-10    214            i*=2;   //  i=i*2      428            System.out.println("i="+i);  //428            }             public void 條件運算子(int 成績){         //三目運算子            //邏輯運算式?運算式1:運算式2        //int 成績=0;         String result;         result=成績>=60?"通過":"沒有通過";         System.out.println("成績:"+result);     }                            public static void main(String[] args){        NewClass oe=new NewClass();        oe.位元運算符();        oe.priorityOfBitOperator();  //oe.調用方法        oe.賦值運算子();        oe.條件運算子(80);    }      }

JAVA位、賦值、條件運算子等

聯繫我們

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