About the shift left operator in C language, the shift left Operator

Source: Internet
Author: User

About the shift left operator in C language, the shift left Operator

Referring to c and pointer, the result of left shifting in c language is the same regardless of arithmetic or logical left shifting. In addition, perform the same operation for the signed and unsigned types, that is, shift N bits to the left Based on the binary bit. For example:

1 clude <stdio.h>2 int main()3 {4     int a = 0xafffffff;5     printf("%d\n",a<<1);6     int b = 0xff;7     printf("%d\n",b<<1);8 }
ubuntu@ubuntu:~/code/2017.8.28$ ./test31610612734510

 

In memory, int variable a is converted to binary storage 1010 1111 1111 1111 1111 1111 1111 1111 as a negative integer, B is stored in the memory as 0000 0000 0000 0000 0000 0000 1111 1111 is a positive integer, the program result is as above. A is converted to a positive integer after being shifted to the left, and B is converted to a positive integer. All values are shifted to the left (including the sign bit) in hexadecimal notation ).

For the right shift, arithmetic or logical shift may be performed for different compilers. Perform arithmetic shift under gcc, that is, for the positive integer to the right shift, the arithmetic shift is the same as the logical shift, the right shift is followed by 0 on the left end, and for the negative integer to the right shift, the arithmetic shift is 1 does not move, add 1 to the left end after right shift.

1 #include <stdio.h>2 int main()3 {4     int a = 0xafffffff;5     printf("%d\n",a<<1);6     int b = 0xff;7     printf("%d\n",b<<1);8 }
ubuntu@ubuntu:~/code/2017.8.28$ ./test3-671088641127

A is used as a negative number, and B is used as a positive number, and 0 is used as a negative number, which proves the above conclusion.

In the left shift and right shift, if the total number of digits of the data type is smaller than int, it is converted to int before the shift operation. If the number of shifts left or right is greater than the number of digits of the Data Type, perform the modulo operation on the number of digits of the Data Type first, and then perform the shift operation on the remainder.

 1 #include <stdio.h> 2 int main() 3 { 4     char a = 0x11; 5     //for(i = 0;i < 100;i++) 6     //{ 7     //    printf("%d\n", a>>i); 8     //} 9     printf("%d\n", a>>(sizeof(int)*8+3));10     printf("%d\n", a>>3);11     printf("%lu\n", sizeof(a>>3));12     printf("%ld\n",sizeof(a)) ;13 }
ubuntu@ubuntu:~/code/2017.8.27$ gcc -o test1 test1.ctest1.c: In function ‘main’:test1.c:9:21: warning: right shift count >= width of type [-Wshift-count-overflow]     printf("%d\n", a>>(sizeof(int)*8+3));                     ^ubuntu@ubuntu:~/code/2017.8.27$ ./test12241ubuntu@ubuntu:~/code/2017.8.27$ 

For a char type data, when sizeof (a> 3) is printed, the output is 4, indicating that the variable a> 3 is int type. The output results of a> 3 and a> (32 + 3) are the same, and the system warns that the shift is greater

The data type width proves the conclusion above.

 

  

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.