Java筆記2(位元運算)

來源:互聯網
上載者:User

標籤:

1、位元運算符:~操作符的使用

 public class Test22{

 public static void main(String[] args){   

                       int number1 = 2;

  /*    符號位:0:代表整數  1:代表負數   

                     0000 0000 0000 0000 0000 0000 0000 0010       

           取反: 1111 1111 1111 1111 1111 1111 1111 1101取反後發現為負數的二進位。

           負數的二進位轉換為十進位:需取反再+1.

           負數的補碼:反碼+1    

                     1000 0000 0000 0000 0000 0000 0000 0010   

                  +                                                                1   

                       ---------------------------------------                 

                     1000 0000 0000 0000 0000 0000 0000 0011------(-3)                

          */

  System.out.println(~2);  } }

2、

 public class Test{

 public static void main(String[] args){  

                                 int number = 8;

  /*  

                     0000 0000 0000 0000 0000 0000 0000 1000(原碼)   

            取反   1111 1111 1111 1111 1111 1111 1111 0111(補碼)  

           負數原碼=負數補碼取反+1   

                     1000 0000 0000 0000 0000 0000 0000 1000 (補碼取反)

                 +                                                                 1  

                             ---------------------------------------   

                     1000 0000 0000 0000 0000 0000 0000 1001 ---》-9   */

  System.out.println(~8);  } }

3、邏輯運算子的使用
 public class LuoJiTest{
            public static void main(String[] args){
                    int number1 = 20;
                    int number2 = 30;
  //--&&:短路與
  //--(number1>number2):false  (number1>10):true
  // false&&true
  System.out.println("&&:短路與:"+((number1>number2)&&(number1>10)));
  //--||:短路或
  // false||true
  System.out.println("||:短路或:"+((number1>number2)||(number1>10)));
  //--!:非運算子
  //number1>number2-> 20>30 --false
  System.out.println("!:非運算子:"+(!(number1>number2))); 
  //--&:單與
  System.out.println("&:單與"+((number1>number2)&(number1>10)));
  //--|:單或
  System.out.println("|:單或"+((number1>number2)|(number1>10)));
  //--^:異或
  //(number1>number2):false (number1>10):true
  //false^true
  System.out.println("^異或:"+((number1>number2)^(number1>10)));
  System.out.println(true^true);
  System.out.println(false^false);
 } 
}

4、實現從鍵盤輸入一個三位整數,並逆序輸出

import java.util.Scanner;
public class HomeWorkTest{
             public static void main(String[] args){
  
  Scanner scan = new Scanner(System.in);
  int number = scan.nextInt();

  int baiWei = number/100;
  int shiWei = number%100/10;
  int geWei = number%100%10;
  System.out.println("起始資料為:"+number);
  System.out.println("轉換之後為:"+geWei+""+shiWei+""+baiWei);
  
 }
}

Java筆記2(位元運算)

聯繫我們

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