The left shift in C is the said logical shift, the right end is 0,
And the right shift is the arithmetic shift, and the left side is the highest bit of the symbol bit. (Some compilers may move to the right and the logical shift, but the same is true), where the right shift symbol bit is the subject.
Therefore, the negative shift to the left, it is possible to become positive, but negative to the right, positive or negative .
/********************************************************************** * Yiwei.cpp *compiler:gcc,vs,vc6.0 Win32 * AUTHOR:WK * Last Update:sun March 10:21:44 AM CST ************************************************************** /#include <stdio.h>int main (int argc, char **argv) {int i = -9,j=9; printf ("-9 storage form:%d%x\n", I, I); printf ("9 storage form:%d%x\n", J, J); int i1 = i >> 3;int i2=i<<3; printf ("9 Right Shift 3 bits:%d%x\n", I1, I1); printf ("9 shift left 3 bits:%d%x\n", I2, i2); int j1=j >> 3; int j2=j<<3; printf ("9 Right Shift 3 bits:%d%x\n", J1, J1); printf ("9 shift left 3 bits:%d%x\n", J2, J2); int i3 = i >> 33;int i4=i<<33; printf ("9 right Shift 33 bits:%d%x\n", i3, i3); printf ("9 shift left 33 bits:%d%x\n", I4, I4); int j3=j >> 33; int j4=j<<33; printf ("9 right Shift 33 bits:%d%x\n", J3, J3); printf ("9 shift left 33 bits:%d%x\n", J4, J4); int i5 = i >> -3;int i6=i<<-3; printf ("9 Right Shift-3 bits:%d%x\n", i5, i5); printf ("-9 left Shift-3 bits:%d%x\n", I6, I6); int j5=j >>-3; int j6=j<<-3; printf ("9 Right Shift-3 bits:%d%x\n", J5, J5); printf ("9 left Shift-3 bits:%d%x\n", J6, J6); int i7 = i >> -33;int i8=i<<-33; printf ("9 right Shift-33 bits:%d%x\n", i7, i7); printf ("9 left Shift-33 bits:%d%x\n", i8, i8); int j7=j >>-33; int j8=j<<-33; printf ("9 right Shift-33 bits:%d%x\n", J7, J7); printf ("9 left Shift-33 bits:%d%x\n", J8, J8); return 0;}
The operating result is:
First of all, the extreme numbers I gave here have moved 33-bit and-33 in the case of int with only 32 bits, and I didn't give move 3,000-bit move-3,000 bit or more, because I think this move 33-bit and-33 bits are enough to represent this situation , We'll analyze the cause of the results.
I started guessing that the right i=n%32 program in the compiler translates large numbers into 32-bit movable ranges on 32-bit platforms (where we move in the range that int can represent)
The following tests were carried out:
#include <stdio.h>int main (int argc, char **argv) { int i =-9; printf ("-9 storage form:%d%x\n", I, I); printf ("\ n"); int i3 = i >> 33;int i4=i>>1; printf ("9 right Shift 33 bits: %d%x\n", i3, i3); printf ("9 Right Shift 1 bits: %d%x\n", I4, I4);p rintf ("\ n"); int i5 = i >> -1;//-1 Complement form: 11111111 111111111 11111111 11111111int i6=i>>63;//63 binary: 00000000 00000000 00000000 00011111int i7=i> >31; printf ("9 Right Shift-1 bits: %d%x\n", i5, i5); printf ("9 right Shift 63 bits: %d%x\n", I6, I6);p rintf ("9 Right shift 31 bits: %d%x\n", i7, i7); printf ("\ n"); int i8 = i >> -33;//-33 complement store: 11111111 11111111 11111111 11011111int i9 = i >> -1;int i10= i >>; printf ("9 right Shift-33 bits: %d%x\n", I9, I9); printf ("9 left-1 bits: %d%x\n", I9, I9);p rintf ("9 left shift 63 bits: %d%x\n", i10, I (ten); return 0;}
The results are as follows:
It was tested again:
/********************************************************************** * Yiwei.cpp *compiler:gcc,vs,vc6.0 *Author : WK ************************************************************************/#include <stdio.h>int main (int ARGC, char **argv) {int i = 9; printf ("9 storage form:%d%x\n", I, I); printf ("\ n"); int i3 = i >> 33;int i4=i>>1; printf ("9 right Shift 33 bits:%d%x\n", i3, i3); printf ("9 Right shift 1 bit:%d%x\n", I4, I4);p rintf ("\ n"); int i5 = i >> -1;//-1 complement form: 11111111 111111111 11111111 11111111in T i6=i>>63;//63 binary: 00000000 00000000 00000000 00011111int i7=i>>31; printf ("9 Right Shift-1 bits:%d%x\n", i5, i5); printf ("9 right Shift 63 bits:%d%x\n", I6, I6);p rintf ("9 Right shift 31 bits:%d%x\n", i7, i7);p rintf ("\ n"); int i8 = i >> -33;//-33 complement store: 11111111 11111111 11111111 11011111int i9 = i >> -1;int i10= i >> 63; printf ("9 right Shift-33 bits:%d%x\n", I9, I9); printf ("9 left Shift-1 bits:%d%x\n", I9, I9);p rintf ("9 left shift 63 bits:%d%x\n", i10, i10); return 0;}
So I made the following analysis:
So I came to a conclusion: 1. When the number of digits to be moved (0 left to the right , and the left fill sign to the right) is shifted normally at 0----32 bits, because the int type is less than 32 bits and does not exceed the range of digits
2. When the number of digits to be moved is greater than 32, the remainder 32 will be converted to a number less than 32 digits, to be shifted
3. When the number of digits to be moved is negative, take a negative complement ( because negative numbers are stored in complementary form in memory ) behind 5 bits ( 5 bits on 32-bit platforms, 64-bit Platform top to take 6 bits, for specific reasons please refer to the assembly code and computer composition principles related knowledge, I do not explain the 5-bit binary according to unsigned parsing, if greater than 32 in accordance with step two to take the remainder 32, if less than 32 to carry out 1 steps to shift .
(if there are errors in the above conclusions, please correct me!) Thank you! )
Thinking about a program 3 (moving negative digits of a shift operation)