C language Logic Shift and arithmetic shift learning notes

Source: Internet
Author: User
Tags arithmetic printf
The shift operation of C language is one of the most frequent operations that programmers use every day, and the shift operation can save us a lot of time and the program can be written more beautifully. We say that the shift operation can be divided into left and right , the left-shift bit operator is <<, the left one indicates that the original value is multiplied by 2( if left to overflow it will not get the correct The Right bitwise operator is >>, and the right one represents dividing the original value by 2. For example, move the left operation as shown in the following illustration:

The left 3-bit is essentially the left 3 bits removed, on the right with 3 0 complement, which is left. Seemingly shifting operation is very simple, in fact there is a pit exists.
If you think the right shift is the same, then you fall out of the pit and the pit is shifted to the right. Because the right shift can be divided into logical right SHIFT (SLR) and arithmetic right shift (SAR) two ways. Because the right shift operation has a problem with a left shift: There are two options when moving from the left to the new bit. One is the logical shift, the bits moved to the left are filled with 0, the other is the arithmetic shift, the bit that is moved in the left is determined by the symbol bit, the sign bit is 1 moves into 1, and the sign bit 0 moves into 0, so the shift can guarantee the positive and negative properties of the original value. If the value 10010110 two digits to the right, the result of the logical shift is 00100101, and the result of the arithmetic shift is 11100101.
Move left without arithmetic left and logical left shift (because arithmetic left and logical left are the same)
The standard notes that all shift operations performed by unsigned values are logically shifted, but whether a logical shift or an arithmetic shift depends on the compiler for a signed value. You can write a simple test program to see what kind of shift your compiler uses, but that doesn't guarantee that other compilers will do the same. Therefore, if a program uses a signed right shift operation, he is not portable .

I did my own test on my PC, and the code was as follows

The code is as follows Copy Code
#include <stdio.h>
int main (int argc, char const *argv[])
{
int a =-4; Computer storage is stored in the form of a complement, so it is 11111100
printf ("Before a=%d\n", a);
A = a >> 1; Move Right One
printf ("After a=%d\n", a); If it's-2, it's arithmetic right, and if it's 126, it's the logical right shift.
return 0;
}


The results of the execution are shown in the following illustration:

Shows that the compiler uses arithmetic to move right in VS2015 environments on Windows.

We may encounter a situation like this:
a << -5
Move left - what does 5 digits mean? Is it a 5 -bit right, or does it shift at all? or the number of bits that are shifted is more than the number of digits in the operand, what happens?
The Standard explains that the behavior of such displacements is undefined, so he is determined by the compiler. So we should try to avoid this, it will cause some of our programs to be unpredictable errors, and use this kind of shift operation is not portable.
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.