java中的運算子

來源:互聯網
上載者:User

標籤:不同   1.5   賦值   符號   多少   資料   bsp   邏輯運算子   十進位   

1算數運算子:+,-,*,/,%2自增自減 :++ ,--3賦值運算子 : = ,+=,-=,*=,/=4關係運算子 : >,<,>=,<=,== ,!=

邏輯運算子: & ,| ,! ,&& ,||

位元運算符:它是對二進位位進行計算。

位元運算符是可以對十進位的整數進行計算,計算方式:將十進位轉為二進位進行計算。

二進位由0和1組成,所以計算的結果要麼是0,要麼是1

1.5位元運算符的符號:

& (與):兩者都是1的時候我就是一,其他都是0

|(或):兩者都為0時才為0 ,其他的都是1.

^(異或):兩者不同為1,相同為0

~(取反):0變為1,1變為0.

//負數的表現形式: 二進位位的最高位為1,那麼這個數就是一個負數。

1111-1111  1111-1111 1111-1111 1111-1111 : -1

0000 0000 0000 0000 0000 0000 0000 0000 : 0

-2:1111-1111  1111-1111 1111-1111 1111-1110

-3:1111-1111  1111-1111 1111-1111 1111-1101

-4:1111-1111  1111-1111 1111-1111 1111-1100

-5:1111-1111  1111-1111 1111-1111 1111-1011

-6:1111-1111  1111-1111 1111-1111 1111-1010

-7:1111-1111  1111-1111 1111-1111 1111-1001

0000-0000  0000-0000 0000-0000 0000-0111  = 7

1111-1111 1111-1111 1111-1111 1111-1001

規律:負數所對應的正數-1 ,取反

  負數所對應的正數取反+1

作用:資料進行加密

123456

0-100 a-z

24845845957512317580960  --- >123456

6移位操作符:對二進位位進行操作

6.1  >>  右移

6.2  <<  左移

6.3  >>> 無符號右移

// >> 右移

// << 左移

// >>> 無符號右移

//右移:

System.out.println(6>>1); // --->3  6/2  2*1

System.out.println(6>>2); // --->1  6/4  2*2

System.out.println(6>>3); // --->0  6/8  2*3

System.out.println(6>>4);

System.out.println(9>>1); // --->4   9/2  2*1

System.out.println(9>>2); // --->2  9/4  2*2

System.out.println(9>>3); // --->1  9/8  2*3

System.out.println(9>>4);//  --->0  9/16  

System.out.println(-6>>1); //  -6/2 -->-3

//規律: 右移多少位,就用需要移動的數除以2的多少次方。

 //左移 :

System.out.println("-----這個是左移的結果----------");

System.out.println(6<<1); // --->12  6*2  2*1

System.out.println(6<<2); // --->24  6*4  2*2

System.out.println(6<<3); // --->48  6*8  2*2*2

System.out.println(6<<4); //---->96  6*16 2*2*2*2

System.out.println(-6<<2);//--->-24

 //規律: 左移多少位,就用這個數乘以2多少次方。

//>>>無符號的右移

System.out.println(6>>>1); //3

System.out.println(6>>>2); //1

System.out.println(-6>>>2); // 1073741822

原理:見圖片。

作用 : 提高運算速率。位元運算是執行最快。

比如: 以最快的方式計算 2*8

  2<<3  == 2*8 = 16

特點 : 一個數A和兩個相同的數^,最後結果還是原來的數A。

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.