Sao bit arithmetic operations

Sao bit arithmetic operations (a) of the four arithmetic

It can be said that bit computing is one thing we have just started to learn the computer will come into contact with. Then bit operation so common, whether we can use it to do some show does it work?

Used operators include the following (java there is an >>>unsigned right):

meaning Operators example
Left (hereinafter complement 0) << 0011 => 0110
Right (leading zero positive, negative S.1) >> 0110 => 0011
Bitwise or 0011 ------- => 1011 1011
Bitwise AND & 0011 ------- => 1011 1011
Bitwise ~ 0011 => 1100
Bitwise XOR (the same is different from 0 to 1) ^ 0011 ------- => 1000 1011

Left right shift
to the right is in addition to the equivalent, to the left is equivalent to multiplication, left one multiplied by two, left two multiplied by 4, and so on, whether positive, negative, they are right, left shift, are unsigned 32-bit right shift of its own,

Bit computing with the four operations (here are considered integer [Java version])

addition

Addition there are two operations: 求和and进位

Sum: 1 + 0 = 1 1 = 1 + 0 + 0 = 0 0

XOR: 1 = 1 ^ 1 ^ 0 ^ 0 = 0 = 0 0 1

Carry: 1 + 1 + 1 = 1 + 0 = 0 0 = 0 0

位与:1&1=1 1&0=0 0&0=0

So this time, we use bit operations to achieve the addition certainly use 异或and位与

  • Recursive version

    // 使用递归实现
    int add(int a,int b){
        // 假如进位为0
        if(b ==0){
            return a;
        }
        // 获得求和位
        int s = a^b;
        // 获得进位,然后需要向做移动一位表示进位
        int c = ((a&b)<<1);
        return add(s,c);
    }
  • Non-recursive version

    Recursive and non-recursive version is no different versions

     int add2(int a,int b){
    
        int s;
        int c;
        while(b != 0){
            // 获得求和位
            s = (a^b);
            // 获得进位,然后需要向做移动一位表示进位
            c = ((a&b)<<1);
            a = s;
            b = c;
        }
        return a;
    }

Subtraction

Subtraction, emm, simple point, is to use the addition to do, just say the addition of a negative number it

First, we have to first minuend negation (a negation that is ~a+1)

int adverse(int a){
    return add(~a,1);
}

The complete code for subtraction

// 取得相反数
int adverse(int a){
    return add(~a,1);
}
// 减法函数 其中add就是前面的加法函数
int subtract(int a,int b){
    return add(a,adverse(b));
}

multiplication

He said front multiplication us first consider the issue under the symbol.

First of all, let's get to know a number is positive or negative

// 负数返回-1,正数返回0
int getsign(int i){
    return (i >> 31);
}

If negative, then be inverted

// 如果为负数,则进行取反
int toPositive(int a) {
    if (a >> 31 == -1)
        // 进行取反
        return add(~a, 1);
    else
        return a;
}
  • Solution one:

    Multiplication, we kid learning multiplication, the teacher told us, is to multiply accumulate, such as 6 * 7 7 6 is the sum of it, so, we can certainly use the addition to achieve multiplication. Time complexity of O (n).

int multiply(int a, int b){
    Boolean flag = true;
    // 如果相乘为正数,flag为false
    if (getsign(a) == getsign(b))
        flag = false;
    // 将a取正数
    a = toPositive(a);
    b = toPositive(b);
    int re = 0;

    while (b!=0) {
        // 相加
        re = add(re, a);
        // b进行次数减一
        b = subtract(b, 1);
    }
    // 假如结果是负数,则进行取反
    if (flag)
        re = adverse(re);
    return re;
}
  • Solution two

    First, look at a picture of it, this picture is because I use the touchpad notebook paintings, the ugly little (en, I can not stand the ugly), binary picture above is multiplied by 2 * 6.

     

    1
    1

     

    Above we can see that multiplied somewhat similar 与&, except that it will carry it. So our problem can be simplified. Phase, and then can carry, and then summed. I want to change the direction: a shift to the left, b move to the right, if the last bit b is 1, can be a plus. (Time complexity O (logn))

    int multiply2(int a, int b) {
        Boolean flag = true;
        // 如果相乘为正数,flag为false
        if (getsign(a) == getsign(b))
            flag = false;
        // 将a取正数
        a = toPositive(a);
        b = toPositive(b);
        int re = 0;
        while (b!=0) {
            // 假如b的最后一位为1
            if((b&1) == 1){
                // 相加
                re = add(re, a);
            }  
            b = (b>>1);
            a = (a<<1);
        }
        // 假如结果是负数
        if (flag)
            re = adverse(re);
        return re;
    }

division

Similar division and multiplication.

  • Solution one

    Solution one division and multiplication of a similar solution, minus the divisor from the dividend above, until the reduction is not enough, just give up. Time complexity of O (n).

  • Solution two
    workarounds two ideas is this: from b*(2**i)开始减(2 ** i represents the i th power of 2), and has been reduced b*(2**0). Such solutions time complexity is O (logn).

    // a/b
    int divide(int a,int b){
       Boolean flag = true;
       // 如果相除为正数,flag为false
       if (getsign(a) == getsign(b))
           flag = false;
       // 将a取正数
       a = toPositive(a);
       b = toPositive(b);
       int re = 0;
       int i = 31;
       while(i>=0){
           // 如果够减
           // 不用(b<<i)<a是为了防止溢出
           if((a>>i)>=b){
               // re代表结果
               re = add(re, 1<<i);
               a = subtract(a, (b<<i));
           }
           // i减一
           i = subtract(i, 1);
       }
       // 假如结果是负数
       if (flag)
           re = adverse(re);
       return re;
    }

ok, today's four-bit arithmetic operations on here, where I did not take into account the case of data overflow, because the focus is not here, the next blog I will talk about some of the show-bit arithmetic operations in the algorithm.

Guess you like

Origin www.cnblogs.com/xiaohuiduan/p/10981210.html
Sao