bitwise operator induction

=== 1. and operation & ===
The and operation is usually used for binary bit operations . For example, the result of a number and 1 is to take the last bit of the binary. This can be used to determine the parity of an integer. The last bit of binary is 0 to indicate that the number is even , and the last bit of 1 indicates that the number is odd.
If both numbers in the same bit are 1, it is 1; if one of them is not 1, it is 0.
00101
11100
(& or and)
----------------
00100
=== 2. OR operation | ===
The or operation is usually used for unconditional assignment . For example, the result of a number or 1 is to force the last bit of the binary to become 1. If you need to change the last digit of binary to 0, you can subtract one after the number or 1. The actual meaning is to even number.
The same bit is 1 as long as one is 1.
00101
11100
(| or or)
----------------
11101
=== 3. xor operation^ ===
The XOR symbol is ^. The bitwise XOR operation performs a logical bitwise XOR operation on each bit of a bitwise OR binary number in an equal-length binary pattern. The result of the operation is that the bit is 1 if it is different, otherwise the bit is 0.
The inverse operation of the xor operation is itself, that is to say, the final result of XORing the same number twice is unchanged, that is, (a xor b) xor b = a. The xor operation can be used for simple encryption. For example, I want to say 1314520 to my MM, but I am afraid that others will know, so the two parties agree to use my birthday 19880516 as the key . 1314520 xor 19880516 = 20665500, I will tell MM 20665500. MM calculated the value of 20665500 xor 19880516 again and got 1314520, so she understood my intention.
If the same bit is different, it is 1, and the same bit is 0.
00101
11100
(^ or xor)
----------------
11001
Operation result
x <- x # y
y <- x @ y
x <- x @ y
After executing the first sentence, x becomes x # y. Then the essence of the second sentence is y <- x # y @ y. Since # and @ are inverse operations of each other, y at this time becomes the original x. In the third sentence, x is actually assigned as (x # y) @ x. If the # operation is commutative, then x becomes the original y after assignment. The result of these three sentences is that the positions of x and y are swapped.
Addition and subtraction are inverse operations, and addition is commutative. By replacing # with + and @ with -, we can write a swap procedure (Pascal) that does not require temporary variables.
procedure swap(var a,b:longint);
begin
a:=a + b;
b:=a - b;
a:=a - b;
end;
Well, didn't you just say that the inverse of xor is itself? So we have a swap process that looks very weird:
procedure swap(var a,b:longint);
begin
a:=a xor b;
b:=a xor b;
a:=a xor b;
end;
Note: The bitwise version of swapping two numbers does not work with the self-swapping of one number. That is, if the "b" of the above program is changed to "a", the result is that the variable a becomes zero. Therefore, when using quick sort, since it involves the self-exchange of a number, if you want to use the bit operation version of the exchange of two numbers, you should judge first. The specific time loss is omitted here.
=== 4. Not operation~ ===
The definition of not operation is to invert all 0s and 1s in memory. Be extra careful when using not, you need to pay attention to whether integer types are signed or not. If the object of not is an unsigned integer (which cannot represent negative numbers), the resulting value is the difference between it and the upper bound of the type, because numbers of unsigned types are represented in order from 00 to $FFFF. Both programs below (different only in language) return 65435.
where
a:word;
begin
a:=100;
a:=not a;
writeln(a);
end.
1
2
3
4
5
6
7
8
#include<stdio.h>
int  main()
{
     unsigned  short  a=100;
     a=~a;
     printf ( "%d\n" ,a);
     return  0;
}
If the object of not is a signed integer, the situation is different, as we will mention later in the section "Storage of Integer Types".
=== 5. shl operation << ===
a shl b means to convert a to binary and then shift left by b bits (add b 0s at the back). For example, the binary of 100 is 1100100, and the conversion of 110010000 to decimal is 400, then 100 shl 2 = 400. It can be seen that the value of a shl b is actually a multiplied by 2 to the b power, because adding a 0 after a binary number is equivalent to multiplying the number by 2.
It is generally considered that a shl 1 is faster than a * 2 because the former is a lower-level operation. Therefore, the operation of multiplying by 2 in the program should be replaced by a left shift as much as possible.
Defining some constants may use the shl operation. You can conveniently represent 65535 as 1 shl 16 - 1. Many algorithms and data structures require that the data size must be a power of 2. In this case, shl can be used to define constants such as Max_N.
=== 6. shr operation >> ===
Similar to shl, a shr b represents a binary right shift by b bits (remove the last b bit), which is equivalent to dividing a by 2 to the b power (rounded). We also often use shr 1 to replace div 2, such as binary search , heap insertion operations, and so on. Finding a way to use shr instead of division can greatly improve the efficiency of the program. The binary arithmetic of the greatest common divisor replaces the surprisingly slow mod operation with a divide-by-2 operation, which can improve the efficiency by 60%.

priority

edit
Among the bitwise operators in C language, they are arranged in order of precedence as
1
~
2
<<、>>
3
&
4
^
5
|
6
&=、^=、|=、<<=、>>=

Simple application

edit
Sometimes our program needs a small Hash table to record the state. For example, when doing Sudoku, we need 27 Hash tables to count the number of each row, each column and each small nine-square grid . At this point, we can record with 27 integers less than 2^9. For example, a small nine-square grid filled with only 2 and 5 is represented by the number 18 (000010010 in binary), and a row with a status of 511 means that the row is full. When we need to change the state, we do not need to convert this number into binary and then change it back, but directly perform bit operations . When searching, expressing the state as an integer can better perform operations such as weight judgment. This question is a classic example of using bitwise speedup in search. We'll see more examples later.
Some common binary bit transformation operations are listed below.
Function | Example | Bit Operations
----------------------+---------------------------+--------------------
Remove the last digit | (101101->10110) | x shr 1
Add a 0 at the end | (101101->1011010) | x shl 1
Add a 1 at the end | (101101->1011011) | x shl 1+1
Change the last digit to 1 | (101100->101101) | x or 1
Change the last digit to 0 | (101101->101100) | x or 1-1
Invert the last bit | (101101->101100) | x xor 1
Change the kth bit of the right number to 1 | (101001->101101,k=3) | x or (1 shl (k-1))
Change the kth bit of the right number to 0 | (101101->101001,k=3) | x and not (1 shl (k-1))
Invert the kth bit of the right number | (101001->101101,k=3) | x xor (1 shl (k-1))
Take the last three | (1101101->101) | x and 7
Take the last k bits | (1101101->1101,k=5) | x and(1 shl k-1)
Take the kth digit from the right | (1101101->1,k=4) | x shr (k-1) and 1
Change the last k bits to 1 | (101001->101111,k=4) | x or (1 shl k-1)
Invert the last k bits | (101001->100110,k=4) | x xor (1 shl k-1)
Change consecutive 1s on the right to 0 | (100101111->100100000) | x and (x+1)
Change the first 0 from the right to 1 | (100101111->100111111) | x or (x+1)
Change consecutive 0s on the right to 1 | (11011000->11011111) | x or (x-1)
Take the right consecutive 1 | (100101111->1111) | (x xor (x+1)) shr 1
Remove the left of the first 1 from the right | (100101000->1000) | x and not (x xor (x-1)) (or x and (-x))
This last one is used in tree arrays.
Hexadecimal representation in Pascal and C
In Pascal, you need to add a $ sign in front of the hexadecimal number, and in C, you need to add 0x in front of it. We will use this frequently in the future.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325867970&siteId=291194637