A bitwise operation represents a binary-based operation.
Bit operations first understand the binary. Binary means that every bit of a number is 0 or 1.
Bitwise operator << (right-shift operator) \ >>(left-shift operator)
With (&) 0 & 0 = 0 1 & 0 = 0 0 & 1 = 0 1 & 1 = 1
or (|) 0 | 0 = 0 1 | 0 = 1 0 | 1 = 1 1 | 1 = 1
XOR (^) 0 ^ 0 = 0 1 ^ 0 = 1 0 ^ 1 = 1 1 ^ 1 = 0
Displacement operator
Bit left shift
The essence of the left shift operation is to shift the binary value of the corresponding data to the left bit by bit, and fill 0 in the vacated position, the highest bit overflow and discard
Example: int a,b;a = 5;b =a<<2; A move left 2 bits//a binary 00000101 to the left 2 bits is 00010100 namely b=20;
People may find it difficult to calculate here, and I'm going to say a calculation that I've seen.
Binary in the left more than one represents 2 of the 1-square more than 2-bit represents 2-square, in turn calculates
If you move to the left, multiply it by the number of 2.
The binary of int a=85,b;b=a<<3;//a is 00000000 01010101 moves to the left three bits 00000101 01010000 here the value of b = 85x2x2x2 = 680 We can count on the computer.
2. Right-shift operator
Here, instead of the left-shift operator, the binary value of the corresponding data is shifted one bit to the right, and the vacated position is filled with 0, the highest bit 0
Example: int A, b;a=5;b=a>>2; A move to the left 2-bit 00000101 00000001//then b=1 we can also use the above method to calculate just division//when the highest bit is 1, according to the computer system, processing mode
Composite operations
operator meaning description
& bitwise AND If two corresponding bits are 1, then the result value of this bit is 1, otherwise 0
| bitwise OR two corresponding bits as long as there is one for 1, the result value of this bit is 1
^ Bitwise XOR If the two bits values of the participating operations are the same, then 0, otherwise 1
~ ~ is a unary operator used to reverse the bitwise of a binary number, 0 to 1, 1 to 0
1, Bitwise AND operator (&)
Refers to the two two data that participate in the operation, in accordance with the binary "and" operation, if two corresponding binary bits 1, then the result of this bit is 1. otherwise 0
Bitwise AND Operation Example: 00000011 (3) &00000101 (5) 00000001 (1) So we know 3&5=1 we can use bits and pairs of memory cells to clear 0 #include <stdio.h> int main () { int a=5; a &= 0x00; return 0; }
Bitwise OR "operator (|)
Two corresponding bits as long as there is a value of 1, the result of this bit is 1. In the case of logic or arithmetic, one is true.
Xor operator (^)
His rule is: if the two bits values of the operation are the same, then 0, otherwise 1
Application: (1) to make a specific bit flip with the number 01111010 (2), want to make it low 4-bit flip, that is, 1 to 0,0 1. It can be "XOR" with 00001111 (2), i.e.: 01111010^00001111 The low 4 bits of the 01110101 operation result are just the flip of the low 4 bits of the original number. As you can see, the few that you want to flip will be 1 to the ∧ operation. (2) Exchange two values without temporary variables such as: A=3, i.e. one (2); b=4, or 100 (2). To swap the values of a and B, you can use the following assignment statement: a=a∧b; B=b∧a; a=a∧b;a=011 (2) (∧) b=100 (2) a=111 (2) (a∧b result, A has become 7) (∧) b=100 (2) b=011 (2) (Result of B∧a, B has become 3) (∧) a=111 (2) a=100 (2) (a∧b result, A has changed into 4)
4, "Inverse" operator (~)
He is a unary operator, which is used to find the binary inverse of integers, that is, 1 of each bits on the operand is changed to 0, and 0 becomes 1.
C Language Learning Drip (iv)