The priority of shift operators is as follows:
Arithmetic operators take precedence over displacement operators and relational operators.
The displacement operator is first-order, and the combination is from left to right.
For example, let the unsigned short integer variable A be 011(the corresponding binary number is 0000000 10065438).
Then: the result of a <<3 is 0110 (the corresponding binary number is 000001001000), and a remains unchanged.
The result of a>& gt4 is 04 (the corresponding binary number is 000000000100), and a remains unchanged.
For another example, let the short integer variable A be -4 (the corresponding binary number is11111165438).
Then: a < < the result is -32 (the corresponding binary number is11111/kloc-0.
The result of a>& gt is-1 (the corresponding binary number is1111116544.
Left-shift and right-shift operations in C language
2006-09-30 13:52
Let's talk about moving left first. Shifting to the left means shifting all the bits of a number to the left by several bits and using the
int I = 1;
i = i & lt& lt2; //Shift the value in I by 2 bits to the left.
That is to say, the binary of 1 is 000...000 1 (here, the number of zeros in front of 1 is related to the number of bits of int, and there are 3 1 zeros in 32-bit gcc), and it is changed to 01000 after 00 .. shifted to the left by two digits.
One problem that needs to be paid attention to is the leftmost sign bit of int type and the situation of moving out. As we know, int is a signed integer, and the leftmost 1 bit is a signed bit, that is, 0 is positive 1 negative, so there will be overflow when shifting, for example:
Int i = 0x40000000// hexadecimal 1000000, binary 0 1000000. ...
i = i & lt& lt 1;
Then, if I shift the 1 bit to the left, it will become 0x80000000, that is, 100000...0000 binary, the sign bit is set to 1, and all other bits are zero, which becomes the minimum value that can be represented by the int type. The value of 32-bit int is -2 147488. In C language, most significant bit is discarded. When 1 is discarded, the value of I becomes 0.
A special case of left shift is that when the number of digits left shift exceeds the maximum number of digits of numerical type, the compiler will use the number of digits left shift to remove the maximum number of digits of the model, and then shift according to the remainder, for example:
Int i = 1, j = 0x80000000// Let int be 32 bits.
i = i & lt& lt33; // 33% 32 = 1 shift left 1, and I becomes 2.
j = j & lt& lt33; // 33% 32 = 1 shift left 1 bit, j becomes 0, and the highest bit is discarded.
When compiling this program with gcc, the compiler will give a warning, saying shift the number of digits left >;; = Type length. So actually I, j shift' is 1 bit, which is the remainder after 33%32. This rule is under gcc, and it is not clear whether other compilers are the same.
In short, moving to the left means discarding the most significant bit and filling the least significant bit with 0.
Let's talk about moving to the right first and understand the reasons for moving to the left, then moving to the right is easier to understand.
The concept of moving right is the opposite of moving left, that is, moving a few places to the right, and the operator is >>.
Shift right and shift left handle sign bits in different ways. For signed integers, such as int type, shifting to the right will keep the sign bit unchanged, for example:
int i = 0x80000000
i = i & gt& gt 1; The value of //i will not become 0x40000000, but 0xc00000000.
That is to say, after the sign bit is shifted to the right, the positive number complements 0, and the negative number complements 1, which is the arithmetic shift to the right in assembly language. Similarly, when the number of bits moved exceeds the length of the type, the remainder will be taken and then one bit will be moved.
Negative number10100110 >; 5 (assuming the word length is 8 bits), you get11111.
In short, in C, the left shift is a logical/arithmetic left shift (both are exactly the same), and the right shift is an arithmetic right shift, which will keep the sign bit unchanged. In practical application, we can use left/right shift to do fast multiplication/Divison according to the situation, which is much more efficient than loop.
In many system programs, it is often necessary to perform operations or processes at the bit level. C language provides the function of bit operation, which makes C language can also be used to write system programs like assembly language.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Operator operation
────────────────────────────
& bit logic and
|-Bit Logic OR
Bit logical exclusive OR
10 bit logic inversion
& gt& gt Move to the right
& lt& lt move left.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Bitwise operation is to detect, set or shift the actual bit in a byte or word. It only applies to character and integer variables and their variants, but not to other data types.
We should pay attention to distinguish between bit operation and logical operation.
1. Bitwise AND operation
Bitwise AND operator "&"This is a binocular operator. Its function is to participate in the operation of the binary sum corresponding to two numbers. The result bit is 1 only if the corresponding two bits are both 1, otherwise it is 0. The numbers involved in the operation appear in the form of complement.
For example: 9&; 5 The writable formula is as follows: 00001001(two's complement of 9)&; 00000101(twos complement of 5) 000001(twos complement of1) can be seen in 9&; 5= 1。
Bitwise AND operation is usually used to clear some bits or keep some bits. For example, clear the upper eight bits of A and keep the lower eight bits. 255 operation (the binary number of 255 is 000000001111165438).
main(){
int a=9,b=5,c;
c = a & ampb;
printf("a=%d b=%d c=%d ",a,b,c);
}
2. Bitwise OR operation
The bitwise OR operator' |' is a binocular operator. Its function is the binary OR of two numbers involved in the operation. As long as one of the two corresponding binaries is 1, the resulting bit is 1. The two numbers involved in the operation are complementary.
For example: 9|5 can be written as follows: 00001001| 000010/.
000011kloc-0/(decimal 13) shows 9|5= 13.
main(){
int a=9,b=5,c;
c = a | b;
printf("a=%d b=%d c=%d ",a,b,c);
}
3. Bitwise XOR operation
Bitwise XOR operator'' is a binocular operator. Its function is that the binary corresponding to the two numbers involved in the operation is different or, and when the corresponding binary is different, the result is 1. The participating operands still appear in the form of complement. For example, 9 5 can be written as the following formula: 0000100100011100 in decimal.
main(){
int a = 9;
a=a^ 15;
printf("a=%d ",a);
}
4. Inverse operation
Negative operator ~ is a monocular operator of right combination. Its function is to negate every binary number involved in the operation bit by bit. For example, the operation of ~ 9 is ~ (0000000000100111165438+.
5. Move left operation
Left shift operator "
6. Move right operation
Right shift operator "> > This is a binocular operator. Its function is to shift all the bits of the left operand of "> > by several bits to the right," >; > the number on the right specifies the number of digits to move. For example, let a= 15, a >;; & gt2 means to move 00000111to the right to 000000 1 1 (decimal 3). It should be noted that for signed numbers, when moving to the right, the sign bit will move with it. When it is a positive number, most significant bit is 0, and when it is a negative number, the sign bit is 1, and whether most significant bit is 0 or 1 depends on the compilation system.
main(){
Unsigned a, b;
Printf ("Enter a number:");
scanf("%d ",& ampa);
b = a & gt& gt5;
b = b & amp 15;
printf("a=%d b=%d ",a,b);
}
Look at another example!
main(){
char a='a ',b = ' b
int p,c,d;
p = a;
p =(p & lt; & lt8)| b;
d = p & amp0xff
c =(p & amp; 0xff00)>& gt8;
printf("a=%d b=%d c=%d d=%d ",a,b,c,d);
}
When bitwise AND or, it is best to use 16, which is expressed in the program as follows: 0x00 1 means 00000 1.
So the most significant bit coercion 1 of character type A is as follows: a=a|0x80. Others can be analogized in reverse!