• 【位运算】把整数以二进制形式打印出来玩玩


    1. 概念

    在现代计算机中所有的数据都以二进制的形式存储在设备中,即0、1两种状态。

    1.1 位运算分类

    运算运算符运算逻辑举例
    &只有两个对应位都为 1时才为 10011 & 0101 = 1000
    I两个对应位至少有一个1时就为 10011 l 0101 = 0111
    异或^只有两个对应位不同时才为 10011 ^ 0101 = 0110
    按位取反~1变0,0变1~ 0011 = 1100
    左移num<< i表示将 num 的二进制表示向左移动 i 位所得的值。0011<< 2 = 1100
    右移num >> i表示将 num 的二进制表示向右移动 i 位所得的值。0101 >> 2 = 0001

    1.2 以二进制形式打印出整数

        public static void printBinary(int a) {
            for (int i = 31; i >=0 ; i--){
                System.out.print((a & (1 << i)) == 0 ? "0" : "1");
            }
            System.out.println();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2. 玩一会二进制

    2.1 把一些常见的整数及运算打印出来看看

    
    public static void main(String[] args) {
            System.out.println("============= 打印5 =========");
            int a = 5;
            printBinary(a);
    
            System.out.println("============= Integer.MAX_VALUE =========");
            int b = Integer.MAX_VALUE;
            System.out.println(b);
            printBinary(b);
            System.out.println(b >> 1);
            printBinary(b >> 1);
            System.out.println(b + 2); // 溢出了
            printBinary(b + 2); // 溢出了
            System.out.println(~b);
            printBinary(~b);
            System.out.println(-b);
            printBinary(-b);
    
            System.out.println("============= Integer.MIN_VALUE =========");
            int c = Integer.MIN_VALUE;
            System.out.println(c);
            printBinary(c);
            System.out.println(~c);
            printBinary(~c);
            System.out.println(-c);// 按位取反+1
            printBinary(-c);// -c = ~c + 1;
    
            System.out.println(b+c);
            printBinary(b + c);
    
            System.out.println("============= 打印 异或运算 =========");
            int d = 24;
            int e = 12;
            printBinary(d);
            printBinary(e);
            System.out.println("-----------------------------------------");
            printBinary(d ^ e);
            printBinary(d ^ 0);
            printBinary(d ^ d);
    
            System.out.println("============= 打印 与或非运算 =========");
            d = 24;
            e = 12;
            printBinary(d);
            printBinary(e);
            System.out.println("-----------------------------------------");
            printBinary(d & e);
            printBinary(d | e);
            printBinary(~d );
            System.out.println("-------------------");
            printBinary(d);
            printBinary(e);
            System.out.println("-----------------------------------------");
            printBinary(d & 0);
            printBinary(d | 0);
    
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    执行结果:

    
    ============= 打印5 =========
    00000000000000000000000000000101
    ============= Integer.MAX_VALUE =========
    2147483647
    01111111111111111111111111111111
    1073741823
    00111111111111111111111111111111
    -2147483647
    10000000000000000000000000000001
    -2147483648
    10000000000000000000000000000000
    -2147483647
    10000000000000000000000000000001
    ============= Integer.MIN_VALUE =========
    -2147483648
    10000000000000000000000000000000
    2147483647
    01111111111111111111111111111111
    -2147483648
    10000000000000000000000000000000
    -1
    11111111111111111111111111111111
    ============= 打印 异或运算 =========
    00000000000000000000000000011000
    00000000000000000000000000001100
    -----------------------------------------
    00000000000000000000000000010100
    00000000000000000000000000011000
    00000000000000000000000000000000
    ============= 打印 与或非运算 =========
    00000000000000000000000000011000
    00000000000000000000000000001100
    -----------------------------------------
    00000000000000000000000000001000
    00000000000000000000000000011100
    11111111111111111111111111100111
    -------------------
    00000000000000000000000000011000
    00000000000000000000000000001100
    -----------------------------------------
    00000000000000000000000000000000
    00000000000000000000000000011000
    
    Process finished with exit code 0
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    2.2 用位运算实现两数交换

    public class SwapTwoNum {
    
        public static void main(String[] args) {
            int[] arr = {0, 1, 2, 3, 4};
            swap(arr, 0, 2);
            printArr(arr); // [ 2 1 0 3 4 ]
    
            swap(arr, 4, 4);
            printArr(arr); // [ 2 1 0 3 0 ]  这里发现地址相同的两个值 异或运算 会将该值置为0
        }
    
    	// 能使用这个方法的前提是: 确保 i j 不相等(所以. 知道就好,尽量别用)
        private static void swap(int[] arr, int i, int j){
            arr[i] = arr[i] ^ arr[j]; // 如果i = j, 运算后结果 arr[i] 为 0 即 arr[j]也为 0
            arr[j] = arr[i] ^ arr[j]; // 相当于 arr[j] = 0 ^ 0;
            arr[i] = arr[i] ^ arr[j]; // 相当于 arr[i] = 0 ^ 0;
        }
    
        private static void printArr(int[] arr){
            System.out.print("[ ");
            for (int i : arr) {
                System.out.print(i + " ");
            }
            System.out.println("]");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
  • 相关阅读:
    Go编程:使用 Colly 库下载Reddit网站的图像
    【斯坦福计网CS144项目】Lab5: NetworkInterface
    巯基SH/氨基NH2/羧基COOH/PEG/蛋白Protein/抗体antibody修饰Au@TiO2 核壳纳米粒子 二氧化钛包裹金表面
    ssm+vue的图书馆书库管理系统(有报告)。Javaee项目,ssm vue前后端分离项目。
    平台工程:构建企业数字化转型的基石
    【Unity3D赛车游戏优化篇】新【八】汽车实现镜头的流畅跟随,以及不同角度的切换
    Spring框架(四)Spring的Bean作用域和生命周期
    Lambda表达式实现方式、标准格式、练习、省略模式、注意事项及和匿名内部类的区别
    坑惨啦!!!——符号冲突案例分析
    Linux基础知识与实操-篇八:定期任务执行与进程任务处理
  • 原文地址:https://blog.csdn.net/m0_37955444/article/details/126189579