Shift operation in C Language

Source: Internet
Author: User

AboutShiftOperation:

1.ShiftThe number of two operations must be an integer expression. The integer is upgraded on both sides of the Operation number. The overall Type of the expression is the same as the number of left operations after the upgrade;

2. LeftShiftExp1 <exp2 expression causes the exp1 bit to be moved left by the number specified by expr2 and 0 at the low end;

3. RightShiftOperator> and leftShiftThe operators are asymmetrical. Returns the left operand.ShiftDepends on the type of the left operand:

(1) If the left operand is unsigned (or a non-negative number with a symbol), the left operand is moved to 0;

(2) If the left operand is a negative number with a symbol, the implementer can add 0 or insert the leftmost displacement of the left operand (usually fill the symbol bit ).

 

# Include <stdio. h>

 

Int main (INT argc, char ** argv)

{

Unsigned char V1 = 3;

Unsigned char v2 = 0x03;

Unsigned char V3 = 0;

 

V3 = V1 <2;

Printf ("First time, V1 = % d/N", V1 );

Printf ("First time, V3 = % d/N", V3 );

 

V3 = V2 <2;

Printf ("Sec time, V2 = 0x % x/N", V2 );

Printf ("Sec time, V3 = 0x % x/N", V3 );

}

Output result:

First time, V1 = 3

First time, V3 = 12

SEC time, V2 = 0x3

SEC time, V3 = 0xc

 

The results indicate two problems:

1. Shift V1, which cannot be changed;

2. If you want to use the value after the shift, you must make the shift to the right and assign the value to other variables or itself.

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.