The bitwise shift operator is an operation that moves the data as a binary number, moving it to the left or right several bits. The bit shift operators are divided into left and right shifts, both of which are binocular operators. The first operation object is the Shift object, and the second one is the number of bits moved.
When shifting, the number of digits removed is discarded, and the number of vacated vacancies is related to left or right deceitful act. If left, the specified number is 0, and if it is moved to the right, it is also related to whether the data being shifted is signed. In the case of unsigned numbers, the total number of supplements is 0, and if the number of symbols is signed, the number of supplements is equal to the original number on the leftmost end of the original number (that is, the original symbol bit). The specific shift rules are shown below.
The priority of the bitwise SHIFT operators is as follows:
• Arithmetic operators take precedence over bitwise shift operators over relational operators
• Bitwise shift operators are peers, and the binding is from left to right
For example, to set the unsigned short integer variable A to 0111 (the corresponding binary number is 0000000001001001),
Then: a<<3 result is 01110 (corresponding binary number is 0000001001001000), a does not change
A>>4 result is 04 (corresponding binary number is 0000000000000100), a does not change
For example, set the short integer variable A to 4 (the corresponding binary number is 1111111111111100),
Then: a<<3 result is 32 (corresponding binary number is 1111111111100000), a does not change
A>>4 result is-1 (corresponding binary number is 1111111111111111), a invariant C language left and right shift operation 2006-09-30 13:52
Let's move on to the left, and the left is to move all the bits of a number to the left several digits, using the << operator in C. For example:
int i = 1;
i = i << 2; Move the value in I to the left 2 digits.
In other words, 1 of the 2 is 000 ... 0001 (here 1 the number of front 0 and the number of the int, 32-bit machine, gcc 31 0), left 2 digits after the 000 ... 0100, that is, 10 in 4, so the left-shift 1-bit is multiplied by 2, then the left-shifted n-bit is multiplied by the n-th of 2 (the signed number is not fully applicable, because the left shift may cause the symbol change, the following explains the reason)
One issue that needs to be noted is the leftmost sign bit of the int type and the removal of the shift. We know that int is a signed number of plastic, the leftmost 1 digits is the sign bit, that is, 0 positive 1 negative, then the shift will occur when the overflow, for example:
int i = 0x40000000; 16 in 40000000, 2 in 01000000 ... 0000
i = i << 1;
So, I move 1 bits to the left and then become 0x80000000, which is the 2 100000 ... 0000, the sign bit is set to 1, the other bits are all 0, the minimum value that the int type can represent, and the 32-bit int is-2147483648, overflow. What happens if I move the I left 1? In c the use of the highest bit of discarded processing method, discarded 1, The value of I becomes 0.
A special case in the left shift is when the left bit exceeds the maximum number of digits of the numeric type, the compiler uses the left-shifted digits to the maximum number of digits of the type, and then shifts by remainder, such as:
int i = 1, j = 0x80000000; Set int to 32 bits
i = i << 33; 33% 32 = 1 Left 1 digits, I into 2
j = J << 33; 33% 32 = 1 Left 1 digits, J to 0, top bit discarded
When compiling this program with GCC, the compiler gives a warning, saying that the left shift number is >= type length. Then actually the i,j moves is 1 bits, that is, the remainder after 33%32. This is the rule under GCC, and it's not clear whether other compilers are the same.
In short, left is: Discard the highest position, 0 to the minimum bit
And then the right to move, understand the left shift of the truth, then the right to move on a better understanding.
The concept of right shift is the opposite of moving left, which is to move several bits to the right, and the operator is >>.
The right shift differs from the processing and the left shift of the sign bit, and for a signed integer, such as the int type, the right shift keeps the symbol bit unchanged, for example:
int i = 0x80000000;
i = i >> 1; The value of I will not become 0x40000000, but will become 0xc0000000
That is, the symbol bit to the right, after a positive number of 0, minus 1, that is, in assembly language, arithmetic right shift. Also, when the moved digits exceed the length of the type, the remainder is taken, and the remaining digits are moved.
Negative 10100110 >>5 (assuming a word length of 8 digits), the resulting 11111101
In short, in C, the left is the logical/arithmetic left shift (the two are exactly the same), and the right is the arithmetic right shift, which keeps the symbol bit unchanged. In practical applications, you can use the left/right shift to do fast multiplication/removal operations, which is much higher than the cycle efficiency.
is often required to perform operations or processing at the bit level in many system programs. C language provides the function of bitwise operation, which makes C language can be used as assembly language to write system program.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Operator Action
────────────────────────────
& bit logic
| bit logic or
^ bit logic or
-bit logic
>> right-shift
<< left-shift
━━━━━━━━━━━━━━━━━━━━ ━━━━━━━━&NBSP
Bitwise operations detect, set, or shift the actual bits in a byte or word. It applies only to character and integer variables and their variants and not to other data types.
We should pay attention to the bitwise and logical operations.
1. Bitwise AND Operation bitwise AND operator "&" is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary phase. Only the corresponding two binary is 1 o'clock, the result bit is 1, otherwise 0. The number of participating operations appears in the complement method.
For example: 9&5 can be written as follows: 00001001 (9 of the binary complement) &00000101 (5 of the binary complement) 00000001 (1 of the binary complement) visible 9&5=1.
Bitwise AND operations are usually used to clear some bits to 0 or retain certain bits. For example, the height of a of eight-digit 0, retain a low eight-bit, can be used for a&255 operations (255 of the binary number of 0000000011111111).
Main () {
int a=9,b=5,c;
c=a&b;
printf ("a=%d\nb=%d\nc=%d\n", a,b,c);
}
2. Bitwise OR OP bitwise OR operator "|" is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary phase or. As long as the corresponding two binary has one for 1 o'clock, the result bit is 1. The two numbers participating in the operation appear in complement.
For example: 9|5 can be written as follows: 00001001|00000101
00001101 (decimal is 13) visible 9|5=13
Main () {
int a=9,b=5,c;
c=a|b;
printf ("a=%d\nb=%d\nc=%d\n", a,b,c);
}
3. Bitwise-XOR or operator "^" is the binocular operator. Its function is to participate in the operation of the two numbers of the corresponding binary dissimilarity or, when the two corresponding binary differences, the result is 1. Participation in the OP arithmetic still appears in complement, for example 9^5 can be written as follows: 00001001^00000101 00001100 (decimal 12)
Main () {
int a=9;
a=a^15;
printf ("a=%d\n", a);
}
4. The negation operator is calculated as a single operator with right binding. The function is to reverse the binary bitwise of the number participating in the operation. For example, ~9 's operation is: ~ (0000000000001001) The result is: 1111111111110110
5. Left shift operation Operator "<<" is the binocular operator. The function of the "<<" to the left of the operands of the binary all left a number of digits, from the "<<" to the right number of digits to specify the number of moves, high drop, low 0. For example: A<<4 means to move the binary of a to the left by 4 digits. such as a=00000011 (decimal 3), 4-bit left, 00110000 (decimal 48).
6. Right shift operation Operator ">>" is the binocular operator. The function is to move the binary of the operands to the left of ">>" to the right several bits, and the number to the right of ">>" specifies the number of digits to move. For example: Set A=15,a>>2 to move 000001111 to the right to 00000011 (decimal 3). It should be explained that for the signed number, the symbol bit will move along with the right shift. When a positive number, the highest bit of 0, while negative, the sign bit is 1, the highest bit is 0 or 1 depends on the compiler system requirements.
Main () {
unsigned a,b;
printf ("Input a number:");
scanf ("%d", &a);
b=a>>5;
b=b&15;
printf ("a=%d\tb=%d\n", a,b);
}
Please look at one more example!
Main () {
Char a= ' A ', b= ' B ';
int p,c,d;
P=a;
p= (p<<8) |b;
d=p&0xff;
C= (P&0XFF00) >>8;
printf ("a=%d\nb=%d\nc=%d\nd=%d\n", a,b,c,d);
}
When bitwise-and-or, it is best to use the 16 binary, which is represented in the program: 0x01 represents 0000 0001
So, the highest bit of character type a forces 1 to be like this: a=a|0x80. Others can be followed by analogy.