1. Basic operations
1. Bitwise AND operation
The bitwise AND operator "&" is a binary operator. Its function is the binary phase AND of each of the two numbers involved in the operation. Only when the two corresponding binary bits are both 1, the result bit is 1, otherwise it is 0. The number involved in the operation appears in the form of complement.
Two classic applications of bitwise AND:
- Make a bit or some bits 0: X & 0XFE
- Take a segment of a number: X & 0XFF
2. Bitwise OR operation
The bitwise OR operator "|" is a binary operator. Its function is the binary phase OR of the two numbers involved in the operation. As long as one of the corresponding two binary bits is 1, the result bit is 1. The two numbers involved in the operation appear in one's complement.
Two classic applications of bitwise OR:
- Make one or several bits 1: X | 0X01 (make the lowest bit 1)
- Put the two numbers together: 0X00FF | 0XFF00 = 0XFFFF
3. Bitwise XOR operation
The bitwise exclusive OR operator "^" is a binary operator. Its function is the XOR of the corresponding binary bits of the two numbers involved in the operation. When the two corresponding binary bits are different, the result is 1, otherwise it is 0.
4. Negate operation
The negation operator ~ is a unary operator with right associativity. Its function is to invert each binary bit of the number involved in the operation.
5. Left shift operation
The left shift operator "<<" is a binary operator. Its function shifts all the binary digits of the operand on the left of "<<" by several digits to the left. The number on the right of "<<" specifies the number of digits to move. The high digits are discarded, and the low digits are filled with 0.
- Shift a number to the left by n places, and get the product of the number and the nth power of 2, that is, X <<= 2 <=> x *= 2^n
6. Right shift operation
The right shift operator ">>" is a binary operator. Its function is to shift all the binary digits of the operand on the left of ">>" right by several digits, and the number on the right of ">>" specifies the number of digits to move.
- Arithmetic shift right is equal to dividing by 2 and rounding down
- Shift a number to the right by n places, and get the quotient of the number and the nth power of 2, that is, X <<= 2 <=> x /= 2^n
Second, algorithm skills
1. State compression
Binary state compression: a method of representing and storing a bool array of length m with an m-bit binary integer.
The following bit operations can be used to access the corresponding subscript elements in the original bool array
-
Get the value corresponding to the lowest 1 of the integer x in the binary representation (lowbit operation)
inline int lowbit(int x){ return x & (-x); }
-
Take out the k-th bit of integer x in binary representation
inline int getk(int x){ return (x >> k) & 1; }
-
Take out the 0~k-1 bit (the last k bit) of the integer x in the binary representation
inline int get(int x){ return X & ((1 << k) - 1); }
-
Invert the kth bit of integer x in binary representation
inline int getk(int x){ return x ^ (1 << k); }
-
Assign the k-th bit of integer x in binary representation to 1
inline int getk(int x){ return x | (1 << k); }
-
Assign 0 to the k-th bit of integer x in binary representation
inline int getk(int x){ return x & (~(1 << k)); }
2. Pairwise transformation
Summary by calculation:
- When n is even, n ^ 1 = n + 1
- When n is odd, n ^ 1 = n-1
Therefore, 0 and 1, 2 and 3, 4 and 5... the operation of XOR 1 constitutes "pairwise transformation".
This property is often used for the storage of edge sets in the adjacency list of graph theory. In a graph with undirected edges, store a pair of positive and negative edges in the n and n+1 positions of the adjacency list array, and you can pass The exclusive OR 1 operation removes the storage location of the side (y, x) opposite to the current side (x, y)