preface

Bitwise operation is an operation based on the binary representation of an integer, that is, the operation is to consider the corresponding binary representation of the integer and the operation of each bit of binary. Commonly used on a total of 6 operators, respectively and (&), or (|), xor (^), take back (~) and left (< <), moves to the right (> >) and the unsigned right shift (> > >, only some parts of the language features, such as Java). Except for &, the other operators are binary operators.

Basic introduction to

The operator algorithm
& The value is 1 only when the corresponding bits of both operands are 1; otherwise, the value is 0
I The value is 0 only when the corresponding bits of both operands are 0; otherwise, the value is 1
^ The value is 1 only if the corresponding bits of the two operands are not equal; otherwise, it is 0
~ Invert each bit of the binary representation of the operand, that is, 0 becomes 1,1 becomes 0
<< The full binary representation of the operands is removed with the high order and the low order filled with 0
>> The full binary representation of the operand is stripped of the low order. If the operand is positive, the high order is filled with 0, and if it is negative, the high order is filled with 1
>>> The complete binary representation of the operands is removed, and 0 is added regardless of whether the operands are positive or negative

Pay special attention to

When performing the displacement operation, the right operation digit shift should not exceed the maximum number of integers, of course, more than the error will not be reported, but different languages have different processing details, if you are interested in the in-depth understanding of computer systems, there is a specific introduction.

Based on using

For convenience, we assume that each number has only 4 bits, half a byte, so it can only be represented- 8 to 7, the following shows the use of each operator:

  1. The use of &

    For 2&3, we first get the complete binary representation of two numbers: 2(0010), 3(0011), and then apply the ampersand rule:

    1 0 0 0

    &

    1 1 0 0

    0 0 1 0 (2)

  2. The use of the |

    For 4 | 1, we also get the number of two binary complete first said: 4 (0100), 1 (0001), and then according to the operations of the rules of the | :

    1 0 0 0

    |

    0 0 0 to 1

    0, 1, 0, 1 (5)

  3. The use of ^

    For 6 ^ 2, we first get the complete binary representation of the two numbers: 6(0110), 2(0010), and then perform the operation according to ^ rules:

    1 1 0 0

    ^

    1 0 0 0

    0 1 0 0 (4)

  4. The use of ~

    For ~3, we still get the full binary representation of 3:3(0011), and then operate on the rules of ~ :

    ~

    1 1 0 0

    1, 1, 0, 0 (-4, because we assumed that the digits are only four, so the highest digit is the sign bit)

    The binary representation of the positive number can be obtained quickly after a little calculation, and the binary representation of the negative number can be obtained by taking the negative number and adding 1. For example, in order to get the binary of -7, we first know that the binary of 7 is 0111, and then perform the reverse operation to get 1000, and then add 1 to get 1001, which is the binary representation of -7.

  5. The use of the < <

    For 1 << 2, we still get the full binary representation of 1:1(0001), and then perform the operation as follows:

    It is necessary to note that << operation is not exactly the same as the left operand * 2 and the right operand. In the computer, due to the limitation of the number of bits, there may be operation overflow. Therefore, for 1 << 3, Follow the above rules to get 1000, and since the highest bit is the sign bit, the result is not 8, but -8. Even for 1 << 4, follow the above rules to get 0000, that is, 1 << 4 is 0, not 16 (normally you should not allow the displacement number to operate on the largest number of integers, for illustration only), If you want to verify this, you can test 0x4000 0000 << 1 and 0x4000 0000 << 2.

  6. The use of > >

    Since >> is a signed right shift, here are two examples: first say 6 >> 2, we get the binary representation of 6:6(0110), and then perform the operation according to >> rules: Firstly, the lower 2 bits are cut off to get 01. Since it is positive (the highest sign is 0), two zeros need to be added to get 0001(1), which is the final result. For -7 >> 1, we first get the complete binary representation of -7: -7(1001), and then eliminate the lower 1 bit to get 100. Since it is negative (the highest sign is 1), we need to add a 1 to the higher one to get 1100(-4), which is the final result.

  7. The use of the > > >

    The only difference between >>> and >> is that when the left-hand operand is negative, when the low value is removed, the high value is also filled with 0 instead of 7. Again, we get the full binary representation of -7: -7(1001), and then we cut off the lower 1 bit to get 100, and then we fill in the higher 0 to get 0100, which is the final result. If you want to verify the code yourself, the 32-bit value of -7 is represented as 0xFFFF FFf5. If the low value is discarded and the high value is filled with 0, 0x7FFF FFFC (2147483644) is the final result.

    Note that both <<, >>, and >>> get the same result when the displacement is 0.

summary

The above is the basic knowledge of bit operation, and the following will continue to summarize the common skills and practical application of bit operation, hoping to communicate with you more, if there is any mistake, please help to point out.