bit shift - Right shifting negative numbers in C -


After text

I have a C code, in which I do the following.

  integer nPosVal = + 0xFFFF; // + NANGailLL = -0xFFFF added for ease of understanding; & Gt; 1, nNegVal & gt -> valid reason  

Now when I try

  added for printf ("% d% d", nPosVal> Gone; 1);  

I'm getting

  32767 -32768  

Is it expected?

I am capable of thinking something like

  65535> & Gt; 1 = (int) 32767.5 = 32767 -65535 & gt; & Gt; 1 = (int) -32767.5 = -32768  

That is, -32767.5 is the circular-32768.

Is this understandable?

It seems that your implementation might have arithmetic bit shift in this system, with two supplemental numbers. Moves all the bits to the right and then fills in the upper bits with a few copies of the last bit. So for your example, treat 32-bit as integer here:

  nPosVal = 000000000000000011111111111111 nNegVal = 11111111111111110000000000000001  

After the shift, you got

  nPosVal = 00000000000000000101111111111111 nNegVal = 11111111111111111000000000000000  

If you convert it back to decimal, you get 32767 and -32,768 respectively.

Effectively a correct change towards negative infinity

Edit:.

Result: According to Section 6.5.7 of the latest, this behavior implementation is dependent on negative numbers E1 >> E2 E1 E2 bit conditions have been transferred. If E1 has an unsigned type or if E1 has a signed type and a non-negative value, then the value of the result is an integral part of the quotient of E1 / 2 E2 . If E1 has a signed type and a negative value, then the resulting value is implementation-defined.

It has been said for them:

The C89 committee confirmed the independence in the implementation given by K & A; There is no need to sign in for the signed changes required, because a requirement like this can slow down the code faster and because the usefulness of signed changes is marginal. (The correct transfer from a negative two to the complementary integer is in the same place not the same as the two divided!)

So in the implementation theory is dependent. In practice, I have never implemented no correct arithmetical changes when signed on the left operand.


Comments

Popular posts from this blog

MVP, design question -

excel - Populate list via a bi-Condition -

iphone - How do I make a UIPickerView in a UIActionSheet -