Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left shifting with a negative shift count

Tags:

c

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.

like image 857
Ishq Avatar asked Feb 09 '11 13:02

Ishq


People also ask

Can you left shift by a negative number?

The left shift and right shift operators should not be used for negative numbers. The result of is undefined behaviour if any of the operands is a negative number. For example results of both 1 >> -1 and 1 << -1 is undefined. If the number is shifted more than the size of integer, the behaviour is undefined.

What is negative shift count?

Negative shift counts are illegal and cause a ValueError to be raised. A left shift by n bits is equivalent to multiplication by pow(2, n). A long integer is returned if the result exceeds the range of plain integers.

Is shifting right negative or positive?

For signed numbers, the sign bit is used to fill the vacated bit positions. In other words, if the number is positive, 0 is used, and if the number is negative, 1 is used. The result of a right-shift of a signed negative number is implementation-dependent.

What happens when you left shift a number?

The number to the left of the operator is shifted the number of places specified by the number to the right. Each shift to the left doubles the number, therefore each left shift multiplies the original number by 2. Use the left shift for fast multiplication or to pack a group of numbers together into one larger number.


2 Answers

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.

like image 55
Lundin Avatar answered Sep 19 '22 19:09

Lundin


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 )

like image 32
Anand Avatar answered Sep 18 '22 19:09

Anand