[C language] shift left and right

Source: Internet
Author: User
Tags integer division

Both the left and right shifts are the concepts of bit operations. We know that computers store data based on binary, so the concept of left and right shifts is important. This article deals with a 32-bit machine.

[left] discard highest bit, 0 minimum bit

The left shift is to move a number of bits to the left in accordance with the binary, with the operator << representation in the C language. For example:

int 1  2// Move the value of X to the left 2 bits

1 binary number corresponding to 000 ... 0001 (the front altogether 31 0), the left shift 2 bits then becomes 000 ... 0100, 4 in decimal. So it seems that the left-shift n-bit equals the N-time multiplied by 2. (The signed number is not fully applicable, as the left shift may cause a change in the symbol, as explained below)

It is important to note that when the left-shift result exceeds the range that the data type can represent, an overflow occurs (resulting in data loss and resulting errors). We know that int is a signed integer, the highest bit is the sign bit, 0 is a positive number, and 1 is negative. Then there will be overflow when the shift occurs, for example:

int 0x40000000 // 16 binary 40000000, 2 binary 0100 ..... 00001;

At this point, X moves 1 bits to the left and then becomes 0x80000000, which is 2 binary 1000 ... 0000, the sign bit is set to 1, becomes the int type variable can represent the minimum value (because the negative number is in the complement, the calculation of the complement: take the inverse plus 1), the value of the decimal is 2147483648, we become overflow. If you move this x again to the left 1 bits, then the highest bit is discarded and the value of the lowest bit complement 0,x becomes 0. This seems like an interesting thing to see, so the simple idea of moving left is not accurate by multiplying the power of 2.

There is a special case in the left shift, when the number of left-shifted digits exceeds the maximum number of digits of that numeric type, the compiler first uses the number of digits left to shift to the maximum number of digits and then shifts the calculation by the remainder. For example:

int 1 0x80000000 // The value of y in binary is 1000 ... 0000//33% 32 = 1 Left 1 bits, X becomes 2 ×//33% 32 = 1 Left shift 1 bit, highest bit discarded, Y becomes 0 

When compiling this program with GCC, the compiler will give a warning, meaning the left shift number >= type length. In fact, the above example X and Y are shifted left by 1 bits, which is the remainder after 33%32. (This rule is related to the compiler, different compilers may not handle the same way) [Move right] discard lowest bit, symbol fill highThe concept of right shift, in contrast to the left shift, refers to moving several bits to the right, in the C language with the operator << representation. The right shift is slightly more complicated than the left shift. We are divided into signed integers and unsigned integers to illustrate. For unsigned integers, the right shift can be directly considered to be an n-squared operation divided by 2, with the concept of integer division, with the low-displacement bits all discarded. For a simple example, the decimal 5 corresponds to the binary representation of 101, and it shifts the 1-bit right to get the number will be 2, that is, the binary representation of 10, the right side of the 1 is discarded directly. For signed integers, the right shift maintains the symbol bit unchanged. For example:
int 0x80000000 // corresponds to binary 1000 ... the value of 00001//x does not change to 0x40000000 (binary 0100 ...). 0000), instead of turning into 0xc0000000 (binary 1100 ...). 0000)

That is, if the symbol bit is moved to the right, the highest bit is 0 if it is a positive number, or if it is a negative number, the highest bit is 1 (in this way it is related to the negative number with the complement store). This is also the arithmetic right shift in assembly language.

Similarly, when the number of bits moved exceeds the length of the type, the remainder is taken first and then the remaining digits are moved.

It is worth pointing out that in C, the left shift is the logical/arithmetic left shift (the two are identical), the right shift is the arithmetic right shift, and the sign bit remains unchanged.

[C language] shift left and right

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.