Shift operation in C Language

Source: Internet
Author: User

I have written a small program that displays the binary representation (complement) of int type data. The Code is as follows:
# Include <limits. h> # include <stdio. h>
Int main (void) {int I = 0; int value =-128; // 0xffffff80 int shift = INT_MIN; // 0x80000000
Printf ("The decimal value is: % d \ n", value );
Printf ("***** \ n"); // print marks
For (I = 0; I <32; ++ I) {if (shift & value) {printf ("1") ;}else {printf ("0 ");}
Shift = shift> 1; // right shift 1 bit} // print bits
Return 0;} the expected output is as follows:
The decimal value is:-128
* *** The actual output is as follows:
The decimal value is:-128
* ** 11111111111111111111111111111111 trace the value of the Variable shift and find that its maximum bit (MSB) is always 1. I suddenly realized that this is the right shift of arithmetic, that is, the symbol bit will be retained. Therefore, the shift type is changed to the (unsigned int) type, which solves this bug. It means that for the unsigned number, the right shift is performed logically, and 0 is entered on the removed bit.
Next we will naturally think of it, is it true that the Left shift of the unsigned number is the logical left shift, and the left shift of the signed number is the arithmetic left shift (that is, the reserved symbol bit? The answer is no. In fact, the logical left shift is the same as the arithmetic left shift, and the symbol bit is not retained.
The result of the right shift operation does not exceed the indicated range. For the number of symbols, if one digit is removed from the right and the other digit is left blank, the value of the original symbol bit must be retained. For the number of unsigned symbols, if there is no symbol bit, the logical right shift is taken.
The Left shift operation is different. It may be out of the indicated range, that is, overflow. For MSB bit 0, MSB-1 bit 1 signed positive number, and MSB bit 1, MSB-1 bit 0 signed negative number, left shift will benefit out (int type as an example, the value range is-2 ^ 31 ~ 2 ^ 31-1. For values greater than or equal to 2 ^ 30 and less than or equal to-2 ^ 30-1, the value is shifted to 1, that is, multiplied by 2, which must be out of the indicated range ), in this case, the symbol bit is changed. Since it has exceeded, what is the significance of retaining the original symbol bit?
In summary, the char/short/int/long (int) types that can perform bitwise operations in C are signed, the unsigned prefix is unsigned, and the two are different in the right shift operation.

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.