Do you really understand bit operations?

1 Introduction

Operators are inevitable in the foundation of any programming language. Operators have assignment operators, comparison operators, logical operators, and bitwise operators. Every programming learner should have learned the first three operators, but not everyone has learned the fourth possibility. The same is true for me. I didn't know bitwise operators that much during the initial learning process. So in this article, I will sort out the use of bitwise operators.

2. Knowledge preparation

Since bit operators operate on binary bits (0101010), it is recommended to read this detailed explanation of original code, complement, and inverse code before reading the following content to learn more about binary bit operations.

3. Detailed explanation of bit operators

There are several bitwise operators as shown in the figure below:
Insert picture description here

1. The bitwise negation operator ~

~It is a unary operation method, inverting each binary bit of the data, that is, changing 1 to 0 and 0 to 1.
example:

00010101 取反后为
11101010

2. Bitwise AND operator &

For the two values ​​involved in the operation, if the two corresponding bits are both 1, the result of the bit is 1, otherwise it is 0. That is, 0 & 0 = 0, 0 & 1 = 0, 1 & 0 = 0, 1 & 1 = 1.
example:

 00101010 (&) 
 00010111 
 00000010

Bitwise AND can be used to clear certain bits. For example, clearing the 2nd and 5th bits of the number 11010110 will allow the number to be ANDed with 11101101 (calculated from the previous bit of the valid digit).

11010110 (&) 
11101101 
11000100

Bitwise AND can be used to take certain specified bits of a certain number. For example, if you want to take the 2nd and 5th digits of the number 11010110, you can perform bitwise AND operation with this number and 00010010.

 11010110 (&)
 00010010 
 00010010

3. Bitwise OR operator |

For the two values ​​involved in the operation, as long as one of the two corresponding bits is 1, the result of that bit is 1. That is: 0 | 0 = 0, 0 | 1 = 1, 1 | 0 = 1, 1 | 1 = 1.
example:

 00101010 ( | ) 
 00010111 
 00111111

The bitwise OR operator can be used to change a specific position to 1.

4. Bitwise exclusive OR operator ^

For the two values ​​involved in the operation, if the two corresponding bits are the same, the result is 0, otherwise it is 1. That is: 0 ^ 0 = 0, 1 ^ 0 = 1, 0 ^ 1 = 1, 1 ^ 1 = 0.
E.g:

00101010 ( ^ )
00010111 
00111101

Through bitwise XOR operation, two values ​​can be exchanged without using temporary variables. For example, to exchange the values ​​of two integers a and b, for example:

a = 11010110,b = 01011001 
a = a ^ b; // a = 10001111 
b = b ^ a; // b = 11010110    
a = a ^ b; // a = 01011001

5. Left shift operator <<

It is used to shift all the binary bits of a number to the left by several bits. For example, a = a<<2, shift each binary bit of a by 2 bits to the left, and add 0 to the right. If a = 00001111, then a<<2 = 00111100.Overflow after high bit left shift, discarding has no effect.
In the case of no overflow, shifting one bit to the left is equivalent to multiplying by 2, and the speed of multiplication is faster than multiplication by shifting left.

6. Right shift operator >>

It is used to shift all the binary bits of a number by several bits to the right. For example, a = a>>2, shift the binary bits of a by 2 bits to the right,The low bit moved to the right is discarded, and the highest bit is moved to the original high value. For example, a = 00110111, then a>>2=00001101; b = 11010011, then b>>2 = 11110100.
Shifting one bit to the right is equivalent to dividing by 2 to get the quotient, and the speed of dividing by right shifting is faster than that of division.

7. Unsigned right shift operator >>>

It is used to unsigned right shift of each binary bit of a number by several digits. It is the same as the operator >>. The lower bit shifted out is discarded, but the difference is that the highest bit is filled with 0. For example, a = 00110111, then a>>>2 = 00001101; b = 11010011, then b>>>2=00110100.

Reference for this article:
"Java Programming/Edited by Tang Dashi"

Guess you like

Origin blog.csdn.net/qq_41262903/article/details/106573533