C language shift operator
The bit shift operator is the calculation of the number of data to the binary to move the data to the left or right. The bit shift operator is divided into left shift and right shift, all of which are double-purpose operators. The first calculation object is a shift object, and the second calculation object is the number of binary bits moved. The calculation object of the bit shift operator, the calculation rules and results, and the combination are shown in Table 2-16. When shifting, the number of digits removed is all discarded, the number of shifted vacancy supplies is related to the left or right flowers. If it is left, the number of supplies is specified is 0; if it is right shift, it is also related to whether the data being shifted is related. If it is not a symbol number, the number of supplies is 0; if it is the number of symbols, the number of supplied numbers is equal to the original number (ie, the original symbol bit) on the leftmost position of the original number. The specific shift rules are as follows. The priority of the bit shift operator is as follows: • The arithmetic operator takes precedence over the bit shift operator, which is preferred to the relational operator, the bit shift operator is the same level, the combination is from left to right, for example, set unsigned short The integer variable A is 0111 (corresponding to the binary number is 0000000000000010010000000000000010010010010010010010010010000001001001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002) It does not change, if the short integer variable A is -4 (the corresponding binary number is 111111111111111100), then: a << 3 result is -32 (corresponding to the binary is 11111111111100000), and a constant A >> 4 results - 1 (corresponding to the binary is 1111111111111111), a constant
Left and right shift calculation in C language
2006-09-30 13:52
Let's talk about the left, left shift is to move all the bits of a number to the left, and use the << operator in C. For example:
INT i = 1; i = i << 2; // put 2 digits left in I
That is, the 2 binding of 1 is 000 ... 0001 (31 machines, 31 machines, 31 machines, GCC), 31 pieces in the GCC becomes 000 after 32-bit machines, GCCs in the GCC ... 0100, that is, 10-based 4, so the left shift 1 bit is equivalent to multiplying 2, then the left shift N bit is multiplied by 2 N times (the number of symbols is not completely applicable, because left shift It is possible to cause symbol changes, explain the reason below)
One problem that needs attention is that the leftmost symbol bit and shift position moved out of the int type. We know, int is a symbolic shape, the leftmost 1 bit is the symbolic bit, ie 0 is 1 loss, then shift Overflow occurs, for example:
INT i = 0x40000000; // 16 Encipheet 400000, 2-based 01000000 ... 0000 i = i << 1;
So, i will become 0x80000000 after 1 bit, that is, 2% 100000 ... 0000, the symbol bit is set 1, the other bit is all 0, which is the minimum that can be represented by the int type. The 32-bit int this value is -2147483648, overflow. If the left is then shifted to 1 bit, what is the case? In the C language, the most high-level processing method is used. After discarding 1, the value of I is changed. It became 0.
A relatively special situation in the left shift is when the number of digits of the left shift exceeds the maximum number of digits of the value type, the compiler uses the maximum number of digits of the left-shifted bit, and then shifts according to the remainder, such as :
INT i = 1, j = 0x80000000; // set int for 32-bit i = i << 33; // 33% 32 = 1 left shift 1 bits, I becomes 2 j = j << 33; // 33% 32 = 1 left shift 1 bit, J becomes 0, the highest position is discarded
When compiling this program with GCC, the compiler will give a Warning, saying the left shift number> = Type length. So actually i, J moves 1 bit, which is 33% 32 after the remainder. In GCC Under this rule, other compilers are not as unclear. In short, the left shift is: Discard the highest bit, 0 replenishment
Besides right, understand the truth of the left shift, then the right shift is better to understand.
The concept of right shift and left shift is the opposite, just move a number of positions, the operator is >>.
Right shift to the symbol bit, the left shift is different. For the symbol integersion, such as the int type, the right shift remains the symbolic position, for example:
INT i = 0x80000000; i = I >> 1; // i value does not turn to be 0x40000000, and it will become 0xC0000000
That is to say, the symbol bit is moved to the right, and the positive number is 0, the negative number is added, which is the arithmetic right shift in the assembly language. When the number of mobile bits exceeds the length of the type, the remainder, then move the remainder Bit.
Negative number 10100110 >> 5 (assuming the word length is 8 bits), then it is 11111101
In short, in C, the left shift is logical / arithmetic left shift (the same is identical), the right shift is the arithmetic right shift, the symbolic is unchanged. In practical applications, the left / right movement can be quickly multiplied by the situation. / Except for operation, this will be much higher than the cycle efficiency.
In many system programs, the bit (bit) level is often required for operation or processing. The C language provides the function of the bit operation, which makes the C language can also be used to write system programs like assembly languages. ━━━━━━━━━━━━━━━━━━ 操作 操作 操作━━─── 操作 操作 操作 操作── - ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ──────> & bit logic and | bit logic or ^ bit logic varies or - bit logic anti >> Right shift << Left shift ━━━━━━━━━━━━━━━━━━ ━━━━━━━━━ 按 Bile operations to detect, set, or shift the actual bits in bytes or words, which only apply to characters and integer variables and their variants, Other data types are not applicable. We must pay attention to the zone assembly and logical operations.
1. Bits and operations Bits and operators "&" are binocular operators. Its function is to participate in the two corresponding binary phases of the calculation. Only when the corresponding two bins are 1, the result is 1, otherwise 0. The number of participation in the calculation appears. For example: 9 & 5 can be writable as follows: 00001001 (9 binary complement) & 00000101 (5 binary complement) 00000001 (1 binary complement) Visible 9 & 5 = 1. Bit and operations are usually used to clear certain digits or hold certain bits. For example, a high eight digits of A, reserve low eight bits, can be used as A & 255 operation (255 binary number to 0000000011111111). Main () {Int a = 9, b = 5, C; C = A & B; Printf ("A =% D \ NB =% D \ NC =% D \ N", A, B, C);}
2. Bit or operator stand or operator "|" is a binocular operator. Its function is to participate in the two corresponding binary phases or in the calculation. As long as the corresponding two bits have one to 1, the result is 1. The two numbers participating in the calculation have occurred with complement. For example: 9 | 5 can be writable as follows: 00001001 | 00000101 00001101 (decimal 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. Pressibility or arithmetic press or operator "^" is a binocular operator. Its function is to participate in the two corresponding two bits of the calculation or, when the two correspondence is different, the result is 1. The number of participation operations still appears with 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. Stermers the opposite operation to seek counter operators ~ is a single operator, with right binding. Its function is to reverse the two bits of the number of participating calculations. For example, the calculation of ~ 9 is: ~ (0000000000001001) Results: 1111111111110110
5. Left-shift operation left shift operator "<<" is a binocular operator. Its function puts all the two bits of the number of arithmeters on the left of the "<<", and the number of mobile bits is specified by the number of "<<" on the right, and the high position is discarded. For example: A << 4 means moving 4 bits to the respective bits of A. Such as A = 00000011 (decimal 3), the left shift 4 bits is 00110000 (decimal 48).
6. Right shift operation right shift operator ">>" is a binocula operator. Its function is to put all the two bits of the number of calculations on the left side of the left, ">>" Specify the number of mobile bits. For example, set a = 15, A >> 2 means that the 0000001111 shift to 00000011 (decimal 3). It should be noted that for the number of symbols, the symbol bit will move along with the right shift. When a positive number, the highest bit is added to 0, while the negative number is 1, the symbol bit is 1, the highest bit is 0 or supplement 1 depends on the compilation system.
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 see another 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 =% D \ ND =% D \ n", A, B, C , d);
When standing and or time, it is preferred to use a 16-based, in the program, in the program: 0x01 means 0000 0001, the highest bit forces 1 for the character type A can be described: a = a | 0x80. Other can be pushed in order!