《程序员数学:位运算》-如何使用二进制计算乘法?

作者:小傅哥
博客:https://bugstack.cn
源码:https://github.com/fuzhengwei/java-algorithms

沉淀、分享、成长,让自己和他人都能有所收获!

一、前言

你是什么时候注意到位运算?

从毕业入职公司看大佬的代码出现 2 << 4 开始?从小白晋升高开读框架的源码看到 MAXIMUM_CAPACITY = 1 << 30; 开始?还是从什么时候开始?

其实二进制的位运算一直在我们那身边,从你开始编写 Hello Word 打印输出时就有二进制流的处理,只不过隐藏的很深不好发现。所以在我们开始意识到代码和二进制的关系往往都是来自于看到可以用二进制完成的计算,包括;二进制计算效率高于乘机,也包括二进制可以更好的体现出你要设置值的大小范围。比如你要设定一个指定范围大小的 Int 值 = 1073741824,那么是给这样一个整数值看起来直观,还是二进制 1<< 30 更直观呢?其实他们两个值是相等的。所以这样的情况下也会有二进制运算的体现。

而小傅哥在学习编程阶段,第一次注意到二进制的运算是关于a、b两个值的互换,如果不引入第三个值就可以完成?

int a = 2, b = 3;
a = a ^ b;
b = a ^ b;
a = a ^ b;

一个 ^ 帽子一样的运算符,就把两个数给替换,替换后 a = 3,b = 2 那它是怎么办到的呢?

^ 异或运算:两个操作数的同位中,如果值相同(都是 0 或者都是 1)则为 0,不同(一个是 0,一个是 1)则为 1

而二进制的运算魅力还远不至于此,还可以完成奇偶判断、有效位计算、乘法、加法等。这些内容的学习可以让我们研发人员,积累编程逻辑和拓展思维模式。接下来小傅哥就带着大家学习一下。

二、位操作介绍

位操作是程序设计中对位数组或二进制数的一元和二元操作。在许多古老的微处理器上,位运算比加减运算略快,通常位运算比乘除法运算要快很多。在现代架构中,位运算的运算速度通常与加法运算相同(仍然快于乘法运算),但是通常功耗较小,因为资源使用减少。

四种基本的位运算包括;与&、或|、非~、异或^

int a = 1; // 0001
int b = 2; // 0010
int c = 4; // 0100
int d = 8; // 1000
int e = 15;// 1111

// 与运算;0001
System.out.println(Integer.toBinaryString(a & e)); // 0001
// 或运算;0011
System.out.println(Integer.toBinaryString(a | b)); // 0011
// 非运算;0101
System.out.println(Integer.toBinaryString(a ^ c)); // 0101
// 异或运算;...11110111
System.out.println(Integer.toBinaryString(~d));

三、位运算案例

1. 获取位值

public int getBit(int number, int bitPosition) {
    return (number >> bitPosition) & 1;
}

2. 设置位值

public int setBit(int number, int bitPosition) {
    return number | (1 << bitPosition);
}

3. 清空位值

public int clearBit(int number, int bitPosition) {
    int mask = ~(1 << bitPosition);
    return number & mask;
}

4. 更新位值

public int updateBit(int number, int bitPosition, int bitValue) {
    int clearMask = ~(1 << bitPosition);
    return (number & clearMask) | (bitValue << bitPosition);
}

5. 偶数判断

public boolean isEven(int number) {
    return (number & 1) == 0;
}

6. 正数判断

public boolean isPositive(int number) {
    if (number == 0) {
        return false;
    }
    return ((number >> 31) & 1) == 0;
}

7. 左移乘二

public int multiplyByTwo(int number) {
    return number << 1;
}

8. 右移除二

public int pideByTwo(int number) {
    return number >> 1;
}

9. 正负交换

public int switchSign(int number) {
    return ~number + 1;
}

10. 乘法运算(有符号)

public int multiply(int a, int b) {
    int multiply = 0;
    while (a != 0 && b != 0) {
        System.out.print("计算步骤(" + (isEven(b) ? "偶数" : "奇数") + "):a(" + String.format("%04d", Integer.valueOf(Integer.toBinaryString(a))) + ") = " + a + " | b(" + String.format("%04d", Integer.valueOf(Integer.toBinaryString(b))) + ") = " + b);
        // b 是偶数:2a * (b/2)
        if (isEven(b)) {
            a = multiplyByTwo(a);
            b = pideByTwo(b);
        }
        // b 奇数
        else {
            // b 正数:2a * (b - 1)/2 + a
            if (isPositive(b)) {
                multiply += a;
                a = multiplyByTwo(a);
                b = pideByTwo(b - 1);
            }
            // b 负数:2a * (b + 1)/2 - a
            else {
                multiply -= a;
                a = multiplyByTwo(a);
                b = pideByTwo(b + 1);
            }
        }
        System.out.println(" | multiply(" + String.format("%04d", Integer.valueOf(Integer.toBinaryString(multiply))) + ") = " + multiply);
    }
    return multiply;
}

11. 乘法运算(无符号)

public int multiplyUnsigned(int number1, int number2) {
    int result = 0;
    int multiplier = number2;
    int bitIdx = 0;
    while (multiplier != 0) {
        if ((multiplier & 1) == 1) {
            System.out.println(number1 + " << " + bitIdx + " = " + (number1 << bitIdx));
            result += number1 << bitIdx;
        }
        bitIdx += 1;
        multiplier = multiplier >> 1;
    }
    return result;
}

12. 一的数量

public int countSetBits(int originalNumber) {
    int setBitsCount = 0;
    int number = originalNumber;
    while (number != 0) {
        setBitsCount += number & 1;
        number >>>= 1;
    }
    return setBitsCount;
}

13. 转换计算

public int bitsDiff(int number1, int number2) {
    return countSetBits(number1 ^ number2);
}

14. 有效位数

public int bitLength(int number) {
    int bitsCounter = 0;
    while ((1 << bitsCounter) <= number) {
        bitsCounter += 1;
    }
    return bitsCounter;
}

15. 幂值判断

public boolean isPowerOfTwo(int number) {
    return (number & (number - 1)) == 0;
}

16. 加法运算(Ripple-carry adder)

public int fullAdder(int a, int b) {
    int result = 0;
    // 计算每次的进位值,1 + 1 = 0010 进位为1。是一种&运算。
    int carryOut = 0;
    System.out.println("| aBit | bBit | carryIn | aiPlusBi | bitSum | carryOut | result |");
    for (int i = 0; i < 32; i++) {
        int aBit = getBit(a, i);
        int bBit = getBit(b, i);
        int carryIn = carryOut;
        System.out.print("|   " + aBit + "  |  " + bBit + "   |       " + carryIn);
        // 加和 - 两个值;如果相同则为0,不相同则为1
        int aiPlusBi = aBit ^ bBit;
        System.out.print(" |        " + aiPlusBi);
      
        // 加和 - 进位;
        int bitSum = aiPlusBi ^ carryIn;
        System.out.print(" |      " + bitSum);
      
        // 进位;同位置 ai & bi = 1 | 与进位 aiPlusBi & carryIn = 1
        carryOut = (aBit & bBit) | (aiPlusBi & carryIn);
        System.out.print(" |  " + carryOut + "(" + Integer.toBinaryString(carryOut) + ")   ");
      
        // 累加;把当前位置计算的值,左移n位
        result = result | (bitSum << i);
        System.out.println(" | " + result + "(" + String.format("%04d", Integer.valueOf(Integer.toBinaryString(result))) + ")|");
    }
    return result;
}

四、常见面试题

展开阅读全文

页面更新:2024-03-30

标签:乘积   目的   正数   奇数   偶数   负数   乘法   位数   程序员   符号   逻辑   数学   数字

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top