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

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

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

    }

执行结果:


============= 打印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

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值