Left shifting with a negative shift count

C

C Problem Overview


What exactly happens here?

a << -5

Obviously it doesn't right shift. But the book I'm reading states: > On one machine, this expression actually does a left shift of 27 bits

My question is; why? What causes a left shift of 27 bits? And what exactly happens when shifting with a negative shift count? Thank you.

C Solutions


Solution 1 - C

Negative integers on right-hand side is undefined behavior in the C language.

ISO 9899:2011 6.5.7 Bit-wise shift operators:

> The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.

Solution 2 - C

As already answered by other members, it produces undefined behavior. What I would like to mention here is that you quote from the book ( "On one machine" ) seems to be partial. It does not generalize the behavior. The book might also have explained that the behavior is undefined as per the standard. BTW, I was just going through "The New C Standard - An Economic and Cultural Commentary" and found this statement :

> The Intel Pentium SAL instruction > (generated by both gcc and Microsoft > C++ to evaluate left-shifts) only uses > the bottom five bits of the shift > amount

This very well explains why a left shift of -5 could result into a left shift of 27 ( for 2's complement representation of negative numbers )

Solution 3 - C

The behaviour is undefined.

In 5-bit binary arithmetic, two's-complement -5 has the same binary representation as unsigned +27, which probably explains that particular platform.

Solution 4 - C

If the value you're shifting is a 32 bit variable, shifting -5 goes in a "loop" and shifts 27 forward. Shifting can only take place in a "unsigned" fashion.

Solution 5 - C

int main()
{
    unsigned int a = 1;
    printf("%u\n",a<<(-1));
    return 0;
}

The output is 2147483648.

Here is my assumption and validation:(just assumption!)

1.The "<<" right operand must be unsigned int type,

so firstly, the (int) “-1” will be cast into (unsigned int) "-1". Cause int type is two's-complement representation, the result will be 2^32-1(unsigned int)

2.Due to number 2^32-1 is larger than the Max displacement digit, 2^32 - 1 will be mod 32, which equals 27

I also tried some other nagetive right operand numbers, and the manual calculation results with assumpted rules will be the same with what product by my IDE.

I am trying to find some supporting offical documents, witch could verificate whether my assumption is right or not. Maybe you can tell me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIshqView Question on Stackoverflow
Solution 1 - CLundinView Answer on Stackoverflow
Solution 2 - CAnandView Answer on Stackoverflow
Solution 3 - COliver CharlesworthView Answer on Stackoverflow
Solution 4 - CarnorhsView Answer on Stackoverflow
Solution 5 - CWu LittleView Answer on Stackoverflow