Arithmetic bit operations

1. & (bitwise AND) operator

The two data participating in the operation perform an "AND" operation based on the binary bits.
Negative numbers participate in bitwise AND operations in two's complement form.

Operation rules : 0&0=0; 0&1=0; 1&0=0; 1&1=1;
That is: the result is "1" only if both two bits are "1" at the same time, otherwise it is 0

For example: 5&6 is 0000 0101& 0000 0110 = 0000 0100.
Therefore, 5&6 is worth 4.

The use of "AND" operation :

  1. Clear. If you want to clear a cell to zero, even if all its binary bits are 0, just AND it with a value whose bits are all zero, and the result will be zero.
  2. Get the specified position in a number
方法:找一个数,对应X要取的位,该数的对应位为1,其余位为零,
此数与X进行“与运算”可以得到X中的指定位。

例:设X=10101110,

取X的低4位,用 X & 0000 1111 = 00001110 即可得到;
还可用来取X的2、4、6位。

2. | (bitwise OR) operator

The two objects participating in the operation perform an "OR" operation based on binary bits.
Negative numbers participate in bitwise OR operations in two's complement form.

Operation rules : 0|0=0; 0|1=1; 1|0=1; 1|1=1;
that is: as long as one of the two objects participating in the operation is 1, its value is 1.

For example: 5 | 6 is 00000101 | 0000 0110 = 00000111 Therefore, 5 | 6 is worth 7.

Special functions of "OR operation" :

  1. Often used to set certain positions of a data to 1.
方法:找到一个数,对应X要置1的位,该数的对应位为1,其余位为零。
此数与X相或可使X中的某些位置1。
例:将X=10100000的低4位置1 ,用X | 0000 1111 = 1010 1111即可得到。

3. ^ (XOR) operator

The two data participating in the operation perform an "exclusive OR" operation based on binary bits.

Operation rules : 0 ^ 0 = 0; 0 ^ 1 = 1; 1 ^ 0 = 1; 1 ^ 1 = 0; that is
: two objects participating in the operation, if the two corresponding bits are "exclusive" (different values), The result of this bit is 1, otherwise it is 0.

The special functions of "XOR operation" :

  1. Flip specific bits to find a number that corresponds to the bits of
    Example: X=1010 1110, flip the last bit of X, and use X ^0000 0001 = 1010 1111 to get it.
  2. XOR with 0, retain the original value.
    Example: X^00000000 = 1010 1110.

4. ~ (negation) operator

A piece of data participating in the operation performs "inversion" operation based on binary bits.

Operation rules : ~1 = 0; ~0 = 1;
that is: inverting a binary number bitwise, that is, changing 0 to 1 and 1 to 0.

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

5. << (left shift) operator

Shift all the binary bits of an operand to the left by a certain number of bits (the left bit is discarded and the right bit is filled with 0).

Example: a = a<< 2 Shift the binary bits of a to the left by 2 bits, add 0 to the right,
shift left by 1 bit and then a = a *2;
if the high bit discarded when shifting left does not contain 1, then shift one bit to the left every time , which is equivalent to multiplying this number by 2.

6. >> (right shift) operator

Shift all the binary digits of a number to the right by a certain number of bits. Positive numbers are padded with 0s on the left, negative numbers are padded with 1s on the left, and the right sides are discarded.
Each right shift of the operand by one bit is equivalent to dividing the number by 2.

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

>>The operator shifts all bits of expression1 to the right by the number of bits specified by expression2. The sign bit of expression1 is used to fill the bits left vacant after the right shift. Bits shifted out to the right are discarded.

For example, when the following code is evaluated, the value of temp is -4:
-14 (ie, 11110010 in binary) shifted two places to the right equals -4 (ie, 11111100 in binary).
var temp = -14 >> 2

7. >>> (unsigned right shift) operator

>>>The operator shifts each bit of expression1 to the right by the number of bits specified by expression2. After the right shift, the vacated bits on the left are filled with zeros. Bits shifted out to the right are discarded.

For example: VAR TEMP = -14 >>> 2
variable TEMP value is -14 (that is, binary 1111111111111111111111110010), and the two digits to the right are equal to 1073741820 (that is, 0011111111111111111111100).

8. Compound assignment operator

Bit operators are combined with assignment operators to form new compound assignment operators. They are:
&=Example: a &=b is equivalent to a=a& b
|=Example: a |=b is equivalent to a=a |b
>>=Example: a >> =b is equivalent to a=a>> b
<<=example: a<<=b is equivalent to a=a<< b
^=example: a ^= b is equivalent to a=a^ b

Operation rules :
Similar to the operation rules of the compound assignment operator mentioned earlier.
Bit operations are performed on data of different lengths.
If two data of different lengths are subjected to bit operations, the system will right-align them and then perform bit operations.

Taking the "AND" operation as an example, the explanation is 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 is ANDed with an int type data, the right end is aligned. Finally, the missing bits on the left side are filled according to the following three situations:

  1. If the integer data is a positive number, 16 zeros are added to the left.
  2. If the integer data is a negative number, 16 1s are added to the left.
  3. If the integer data is an unsigned number, 16 zeros are also added to the left.

Guess you like

Origin blog.csdn.net/guoxulieying/article/details/132424018