Java笔记2(位运算)

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);
  
 }
}

    原文作者:位运算
    原文地址: https://www.cnblogs.com/Ly426/p/4718614.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞