Move left and right in the C language of "go"

Source: Internet
Author: User

first, move left.,moving left is to move all the bits of a number to the left by several bits,in theCin use<<operator.For example:

int i = 1;
i = i << 2; //
putIthe value in the left shift2bit

Which means, 1of the2the binary is000...0001 (here1Front0the number andintthe number of digits related,-bit machines, GCCin a toa0),Move left2bit then becomes000...0100,alsoTenin-process4,so move left.1bit equals multiplied by2,so move left .Nbit is multiplied by2of theNthe second party.(signed numbers are not fully applicable,because the left shift has the potential to cause symbolic changes,Explain the reasons below)

one problem to be aware of isinttype leftmost sign bit and shift move out of the case.We know, intis a signed number of shaping,The left-most1bit is sign bit,that0is1Negative,then there will be overflow when the shift occurs.,For example:

int i = 0x40000000;
-
in-process40000000,to be2in-process01000000...0000
i = i << 1;

so, Imove to the left1bit will then become0x80000000,also2in-process100000...0000,sign bit is placed1,the other bits are all0,Become aintthe minimum value that the type can represent,bit ofintThis value is-2147483648,Overflow.If you continue toIMove left1What would happen to a bit??in theCThe processing method of discarding the highest bit is used in the language,dropped the1after, Ithe value becomes a0.

a special case in the left shift is when the number of left-shifted digits exceeds the maximum number of digits of that numeric type,The maximum number of digits that the compiler will use to move left to modulo type,and then shift by the remainder .,as:

int i = 1, j = 0x80000000; //
Setintto be +bit
i = i << 33; % = 1
Move left1bit, Ibecome2
j = J << 33; % = 1
Move left1bit, Jbecome0,The highest bit is discarded

in useGCCwhen compiling this program, the compiler will give you aWarning,say left shift number>=type length.so actuallyi,jThe move is1bit,also33%32after the remainder.in theGCCthat's the rule .,The other compilers are all the same, and it's not clear ..

Anyway, move left isDiscard Highest bit, 0fill the lowest bit

say right Shift .,I understand the reason for moving left.,then the right shift is better understood..

The concept of right shift and left shift opposite,is to move some bits to the right.,operator is>>.

move right to symbol bit processing and left shift different,for a signed integer,likeinttype,move right will keep the sign bit constant,For example:

int i = 0x80000000;
i = i >> 1; I
the value does not become0x40000000,and will become0xc0000000

means,symbol bits move right after,positive words complement0,Negative complement1,that is, the arithmetic right shift in assembly language.also when the number of bits moved exceeds the length of the type,will take the remainder,then move the remainder of the digits.

Negative number10100110 >>5 (assuming that the word length is8bit), you get the11111101

anyway,in theCin,left shift is logical/Arithmetic left shift(The two are exactly the same),shift Right is arithmetic right shift,will keep the sign bit constant.the actual application can be left-based/move right to do fast multiply/except Operations,that would be a lot more efficient than recycling .

References:http://hi.baidu.com/todaygoodhujun/blog/item/b8c10dd15ae4dfd3572c8417.html







C
the shift operation in the language, the content is not many. But in some places you don't pay attention, you neglect.  

First do two small questions first.  

  (1) unsigned char x=3;

  x<<1How much is it? x>>1How much is it?  

  (2) char x=3;

  x<<1How much is it? x>>1How much is it?  

  (3) char x=-3;

  x<<1How much is it? x>>1How much is it?  

  3written in binary number is00000011;-3written in binary number is(complement) 11111101.  

when the program executes, it is the encoded representation of the numeric value, which is the binary representation of the value in memory. For example, the program takes-3, take it .11111101.  

  (1)for unsigned numbers3speaking,x<<1move left one, the leftmost displacement is dropped, the rightmost bit is shifted in 0. Become00000110, so the result is6;x>>1move to the right, because it is an unsigned number, so the logical right shift, the rightmost one to move away, the leftmost move in the bit 0, into00000001, so the result is1.

  (2)for a signed number3speaking,x<<1move left one, the leftmost displacement is dropped, the rightmost bit is shifted in 0. Become00000110, so the result is6;x>>1one bit to the right, because it is a signed number, the logical right shift may occur, and the arithmetic right shift may occur ,Cthe standard does not explicitly specify whether to use logical right SHIFT or arithmetic right shift. But most machines use arithmetic to move right, to become00000001, so the result is still1. But please note that this is just saying that most of the machines are like this, do you dare to make sure that you do not encounter special situations?  

  (3)for a signed number-3speaking,x<<1move left one, the leftmost displacement is dropped, the rightmost bit is shifted in 0. Become11111010, the result is-6. Move right one bit, because there are signed numbers, logical right shifts may occur, and arithmetic right shifts can occur. Most machines use arithmetic to move right, turning11111110, the result is-2.  

Summary: Always shift and fill 0 when moving left. When moving right, the unsigned number is shift and 0, which is now called logical right SHIFT;The number of symbols in most cases is the shift and fill the leftmost bit (that is, the most significant bit), move a few to fill several,This is called arithmetic right shift.  

code with byte encoding in print memory

void Print_char (char x)

{

unsigned char * bp= (unsigned char *) &x;

int size=sizeof (x);

for (int i=0; i<size; i++)

printf ("%.2x", Bp[i]);

printf ("\ n");

}  

Move left and right in the C language of "go"

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.