Inverse complement and bit operation



You can often see bit operations when looking at set expansion, so turn it over and review


1. Original code, complement code, inverted code

  • Original code: convert the value into binary, the highest bit represents the sign bit
  • Inverse code: On the basis of the original code, the positive number is unchanged, the negative sign bit is unchanged, and the rest are inverted
  • Complementary code: On the basis of the original code, the positive number is unchanged, the negative sign bit is unchanged, and the remaining bits are inverted and then added by 1 (ie, the inverted code + 1)

The three are different forms of data storage by computers, and computers use complementary codes to store data. And the computer can use these three to add and subtract


Inverse code:

1+(-1) = [0000 0001]反 + [1111 1110]反 = [1111 1111]反 = [1000 0000]原 = -0

The reverse code appears [0000 0000] The original and [1000 0000] The original two codes indicate positive and negative 0, the complement appears

Complement:

1+(-1) = [0000 0001]补 + [1111 1111]补 = [0000 0000]补 = [0000 0000]原

Solved the different problems of positive and negative 0 encoding




2. Bit operation

Bit operations are performed on the underlying binary system

symbol description Calculation rules
& versus Both are 1, the result is 1
| or Both are 0, the result is 0
^ XOR Same as 0 Different 1
~ Negate 0 becomes 1, 1 becomes 0
<< Shift left Each binary bit is shifted to the left by a few bits, the high bit is discarded, and the low bit is filled with 0s. Means power of 2
>> Right shift Each binary bit is shifted to the right by a few bits, and the unsigned number is filled with 0 in the upper bits. Means divide by 2
Signed number, the processing method of each compiler is different, some complement the sign bit (arithmetic right shift), and some complement 0 (logical right shift)
>>> Logical shift right Regardless of the sign bit, directly shift to the right, zero padding



3. Actual operation


Judging parity

// 最末尾为0偶数,1奇数
public static void main(String[] args) throws IOException {

    int a = 4;
    if( (a & 1) == 0){
        System.out.println("偶数");
    }else{
        System.out.println("奇数");
    }
}

Modulo

h & (length-1)
    
// 其实很简单,主要看length
// &运算,就算h全部为1,&之后都是看length有1的部分,高位全为0
// 那么最大只能是length,所以范围限定在了length里,比 % 运算快多了
// -1为了符合数组0开始
// 这也是扩容为2次幂的原因,配合取模运算

Power of 2 (only one 1, other bits are 0)

1---0001
2---0010
4---0100
8---1000

Fast power

int poww(int a,int b){
    int res = 1;
    int base = a;
    
    while(b != 0){
        if( b & 1 != 0){	// 找到二进制尾数为1的
            res *= base;	// 结果乘于幂
        }
        base *= base;		// 这里呢,一直乘,模拟次幂递增,如果上面为1了,那么这样就会对应到乘于几次幂
        b >>= 1;
    }
    return res;
}

Bitwise addition

public int Add(int num1,int num2) {
        
    while(num2 != 0){
        int sum = num1 ^ num2;  // 计算有单个1的位(0和0位不用计算了,1和1进位给下面操作了)
        int carry = (num1 & num2) << 1;  // 进位操作
        num1 = sum;
        num2 = carry;
    }
    return num1;
}


Guess you like

Origin www.cnblogs.com/Howlet/p/12706396.html