Overview of bit operations (&, |, ^, ~, >>, <<)

Guide All data from modern computers is stored in the device in binary form. That is, the two states of 0 and 1, the operations (+, -, *, /) performed by the computer on the binary data are all called bit operations, that is, the sign bit participates in the operation of the operation.

Overview of bit operations (&, |, ^, ~, >>, <<) Overview of bit operations (&, |, ^, ~, >>, <<)

1. Overview of bit operations

All data from modern computers is stored in the device in binary form. That is, the two states of 0 and 1, the operations (+, -, *, /) performed by the computer on the binary data are all called bit operations, that is, the sign bit participates in the operation of the operation.

There is no reason to say that, let’s take a simple example to see how the CPU performs calculations, such as this line of code:

int a = 35;
int b = 47;
int c = a + b;

Calculate the sum of two numbers, because the computer is all performed in binary, so the int variable we gave above will be converted to binary in the machine and added:

35:  0 0 1 0 0 0 1 1
47:  0 0 1 0 1 1 1 1
————————————————————
82:  0 1 0 1 0 0 1 0

Therefore, compared with the direct use of (+, -, *, /) operators in the code, the reasonable use of bit operations can significantly improve the execution efficiency of the code on the machine.

2. Overview of bit operations

symbol description Algorithm
& versus When both bits are 1, the result is 1
| or When both bits are 0, the result is 0
^ XOR The same two bits are 0, and the difference is 1
~ Negate 0 becomes 1, 1 becomes 0
<< Shift left All binary bits are shifted to the left by several bits, the high bits are discarded, and the low bits are filled with 0
>> Shift right All binary bits are all shifted to the right by several bits. For unsigned numbers, high bits with zeros, and signed numbers, the processing methods of each compiler are different, some complement the sign bit (arithmetic shift right), and some complement 0 (logical shift right)

3. The bitwise AND operator (&)#

Definition: The two data participating in the operation are ANDed by binary bits.

Operation rules:

0&0=0  0&1=0  1&0=0  1&1=1

Summary: If both bits are 1 at the same time, the result is 1, otherwise the result is 0.

For example: 3&5 means 0000 0011& 0000 0101 = 0000 0001, so the value of 3&5 is 1.

Note: Negative numbers participate in the bitwise AND operation in the form of complement.

The purpose of AND operation:

1) Clear

If you want to clear a unit, even if all its binary bits are 0, as long as it is ANDed with a value whose bits are all zero, the result is zero.

2) Take the designated place of a number

For example, to take the lower 4 bits of the number X=1010 1110, you only need to find another number Y, set the lower 4 bits of Y to 1, and the remaining bits to 0, that is, Y=0000 1111, and then perform the bitwise AND operation of X and Y (X&Y=0000 1110) The designated position of X can be obtained.

3) Determine the parity

As long as the decision is based on whether the least significant bit is 0 or 1, a 0 is an even number, and a 1 is an odd number. Therefore, if ((a & 1) == 0) can be used instead of if (a% 2 == 0) to determine whether a is an even number.

4. Bitwise OR operator (|)

Definition: The two objects participating in the operation are subjected to "or" operation according to binary bits.

Operation rules:

0|0=0  0|1=1  1|0=1  1|1=1

Summary: As long as one of the two objects participating in the operation is 1, its value is 1.

For example: 3|5 is 0000 0011| 0000 0101 = 0000 0111, so the value of 3|5 is 7. 

Note: Negative numbers participate in bitwise OR operation in the form of complement.

The purpose of the OR operation:

1) It is often used to set some bits of a data to 1

For example, if you set the lower 4 bits of the number X=1010 1110 to 1, you only need to find another number Y, set the lower 4 bits of Y to 1, and the remaining bits to 0, that is, Y=0000 1111, and then press X and Y Bit-OR operation (X|Y=1010 1111) can be obtained.

5. XOR operator (^)

Definition: The two data participating in the operation are subjected to "exclusive OR" operation according to binary bits.

Operation rules:

0^0=0  0^1=1  1^0=1  1^1=0

Summary: For two objects participating in the calculation, if the two corresponding bits are the same, it is 0, and the difference is 1.

Several properties of XOR:

  1. Commutative law
  2. Law of associativity (a^b)^c == a^(b^c)
  3. For any number x, x^x=0, x^0=x
  4. Reflexiveness: a ^ b ^ b = a ^ 0 = a;

The purpose of the exclusive OR operation:

1) Flip the designated bit

For example, to flip the lower 4 bits of the number X=1010 1110, you only need to find another number Y, set the lower 4 bits of Y to 1, and the remaining bits to 0, that is, Y=0000 1111, and then X and Y are XORed Calculate (X^Y=1010 0001) to get.

2) The value of XOR with 0 remains unchanged

For example: 1010 1110 ^ 0000 0000 = 1010 1110

3) Exchange two numbers

Instance

void Swap(int &a, int &b){
    if (a != b){
        a ^= b;
        b ^= a;
        a ^= b;
    }
}

6. The negation operator (~)

Definition: A piece of data that participates in the calculation is "inverted" in binary.

Operation rules: 

~1=0
~0=1

Summary: Invert a binary number bit by bit, that is, 0 becomes 1 and 1 becomes 0.

The purpose of the exclusive OR operation:

1) Make the lowest bit of a number zero

Let the lowest bit of a be 0, which can be expressed as: a & ~1. The value of ~1 is 1111 1111 1111 1110, and then press "AND" operation, the lowest bit must be 0. Because the "~" operator has higher precedence than arithmetic operators, relational operators, logical operators and other operators.

7. Left shift operator (<<) Definition: Shift all binary bits of an operand to the left by several bits (the binary bits on the left are discarded, and the right is filled with 0). Set a=1010 1110, a = a<< 2 Shift the binary bit of a to the left by 2 bits, and add 0 to the right, that is, a=1011 1000. If the high bit discarded when shifting left does not contain 1, each left shift is equivalent to multiplying the number by 2. 8. Right shift operator (>>)

Definition: Shift all the binary digits of a number to the right by a few bits, add 0 to the left for positive numbers, add 1 to the left for negative numbers, and discard on the right.

For example: a=a>>2 Shift the binary digit of a by 2 digits to the right, and add 0 to the left or 1 to the left, depending on whether the shifted number is positive or negative.

Each shift of the operand by one bit to the right is equivalent to dividing the number by 2.

10. Compound assignment operator

Bitwise operators are combined with assignment operators to form a new compound assignment operator. They are:

&=        例:a&=b    相当于     a=a&b

|=        例:a|=b    相当于     a=a|b

>>=      例:a>>=b   相当于     a=a>>b

<<=      例:a<<=b     相当于      a=a<<b

^=        例:a^=b    相当于   a=a^b

Operation rules: Similar to the operation rules of the compound assignment operator mentioned earlier.

Data of different lengths are subjected to bit operation: If two data of different lengths are subjected to bit operation, the system will align the two to the right and then perform bit operation.

Take "AND operation" as an example to explain as follows: We know that in C language the long type occupies 4 bytes, and the int type occupies 2 bytes. If a long type data and an int type data are "ANDed", the right end is aligned After that, the insufficient bits on the left are made up according to the following three situations,

  1. If the integer data is positive, add 16 zeros to the left.
  2. If the integer data is negative, add 16 ones to the left.
  3. If the shaping data is an unsigned number, add 16 zeros to the left.

Such as: long a=123; int b=1; calculate a& b.
Such as: long a=123; int b=-1; calculate a& b.

Such as: long a=123; unsigned intb=1; calculate a & b. Linux should be learned like this

Guess you like

Origin blog.csdn.net/Linuxprobe18/article/details/113103307