Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Common usage of bit operation
Common usage of bit operation
The two data involved in the operation are AND-operated according to the binary bits.

Operation rule: 0&; 0=0; 0 & amp 1=0; 1。 0=0; 1。 1= 1;

That is, if both digits are "1", the result is "1", otherwise it is 0.

For example: 3&; 5 is 00000011&; 00000101= 0000001Therefore, 3&; The value of 5 is 1.

In addition, negative numbers participate in bitwise AND operation in the form of complement.

Special purpose and operation:

Method: Find a number corresponding to the bit to be taken by X. The corresponding bit of this number is 1, and the rest bits are zero. The bit specified in X can be obtained by AND operation of this number and X. ..

The two objects involved in the operation are OR-operated by binary bits.

Operation rule: 0 | 0 = 0; 0| 1= 1; 1|0= 1; 1| 1= 1;

That is, as long as one of the two objects involved in the operation is 1, its value is 1.

For example, 3|5 means 0000011| 000001= 000011Therefore, the value of 3|5 is 7.

In addition, negative numbers participate in bitwise OR operation in the form of complement.

Special functions of OR operation:

(1) is often used to check some positions of a data.

Methods: Find a number 1 corresponding to X, the corresponding bit of this number is 1, and the rest bits are zero. This number is in phase with x or can be placed in x 1.

Example: take the low order of X= 10 100000 as 1, and use x | 00001165438 =10.

The two data involved in the operation are XOR by binary bits.

Operation rule: 0 0 = 0; 0^ 1= 1; 1^0= 1; 1^ 1=0;

That is, if two corresponding bits of two objects participating in the operation are XOR (with different values), the result of this bit is 1, otherwise it is 0.

Special function of XOR operation;

(1) Flip a specific bit and find the number corresponding to each bit to flip X. The corresponding bit of this number is 1 and the rest bits are zero. This number can only be XOR with the corresponding bit of x.

Example: x =1010110, flip the lower four digits of x and use x000011=/kloc.

(2) different from 0 or, keep the original value, x0000000 =101111.

Let's focus on bitwise XOR. XOR is actually a non-carry addition, such as 1+ 1=0, 0+0=0, 1+0= 1.

Some properties of XOR:

1, commutation law

2, the law of association (that is, (a b) c == a (b c))

3. for any number x, there are x x=0 and x 0 = X

4. Reflexivity: ABB = A0 = A;;

XOR is most commonly used in polynomial division, but its most important property is reflexivity: A XOR B XOR B = A, that is, for a given number A, after two XOR operations with the same operational factor (b), A itself is still obtained. This is a magical property, with which many interesting applications can be obtained. For example, all program textbooks will point out to beginners that to exchange the values of two variables, an intermediate variable must be introduced. However, if you use XOR, you can save the storage space of variables. If there are two variables A and B, and the stored values are A and B respectively, the following three lines of expressions will exchange their value expressions (values):

a=a^b;

b=b^a;

a=a^b;

1- 1000 is placed in an array containing 100 1 elements, only one element has duplicate values, and the others only appear.

Once. Each array element can only be accessed once. Design an algorithm to find out. There is no empty secondary storage.

Suddenly, can you design an algorithm to realize it?

Solution 1. Obviously, someone has put forward a wonderful solution, adding up all the figures and subtracting the sum of 1+2+ ...+1000.

This algorithm is perfect enough. I believe that the standard answer of the questioner is this algorithm. The only problem is that if the sequence is too large, it may lead to overflow.

Option 2: XOR does not have this problem and has better performance.

XOR all the numbers, and the result is XOR with the result of 1 2 3 ... 1000, and the result is a duplicate number.

Application Example 2 (Comprehensive &; And): (topic link: /problem.php? cid= 105 1。 pid=7)

In a series of figures, except for two figures, all the other figures have appeared twice. Find these two numbers and output them in descending order. For example, 2 2 1 1 3 4. The final outputs are 3 and 4.

The problem is that you can always find the product of these two numbers by XOR when you input these two numbers, such as x = 3 4 in the above example;

Then find a variable tmp to separate these two numbers. If bitwise AND is used, we can find that these two numbers will be separated and stored in num 1 and num2 respectively. And then we'll get the results.

Move all the binary bits of the operand to the left by several bits (the binary bits on the left are discarded and the ones on the right are filled with zeros).

For example: a = a

A = a * 2; after left shift 1 bit;

If the high bits discarded during the left shift do not include 1, each left shift is equivalent to multiplying the number by 2.

The binary bits of a number are all shifted to the right by several bits, positive numbers are left supplemented by 0, negative numbers are left supplemented by 1, and right discarded.

Every right shift of the operand is equivalent to dividing the number by 2.

For example, a = a>& gt2 shifts the binary bit of A by 2 bits to the right,

Left complement 0 or complement 1 depends on whether the shifted number is positive or negative.

Bit operator and assignment operator are combined to form a new compound assignment operator. They are:

Operation rule: the operation rule similar to the compound assignment operator mentioned above.

Perform bit operations on data of different lengths.

If two data with different lengths are bitwise operated, the system will align them at the right end and then bitwise operated.

Taking AND operation as an example, the explanation is as follows: We know that in C language, long type takes up 4 bytes and int type takes up 2 bytes. If data of type long and data of type int are AND-operated, after the right end is aligned, the insufficient bits on the left side are supplemented 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 1 to the left.

(3) If the plastic data is unsigned, 16 zeros are added to the left.

Such as: Dragon A =123; int b = 1; Calculate a &;; B.

Such as: Dragon A =123; int b =- 1; Calculate a &;; B.

Such as: Dragon A =123; Unsigned intb =1; Calculate a &;; B.