0

I am reading about the bit shift operation in C/C++ and I found this guide which describes both left shift and right shift operations. It states that

The right operand should not have a negative value or a value that is greater than or equal to the width in bits of the expression being shifted. The result of bitwise shifts on such values is unpredictable.

What exactly does this mean?

I did a test to see what would happen if I attempted to do a left shift of 10, -1, and 80 on an integer:

//Test.cpp
#include <iostream>
#include <string>

using namespace std;

int main()
{

  int myInt = 16;
  int shifted; 
  cout << "Before left shift: " << myInt << endl;

  shifted = myInt << 10;

  cout << "After left shift 10: " << shifted << endl;        //A

  shifted = myInt << -1;

  cout << "After left shift by -1: " << shifted << endl;     //B

  shifted = myInt << 88;

  cout << "After left shift by 80: " << shifted << endl;     //C
}

As expected, the output at A is 16384. At B, the output is 8, which seems to suggest that -1 left shift is equivalent to right shift by 1. At C, the output is 0. I'm not sure why that is.

So just how unpredictable are the results? Is it generally true that a negative left shift is equivalent to a right shift of the same absolute number of positions? Also, what exactly happens when the shift exceeds the width of the type?

Oloff Biermann
  • 706
  • 5
  • 18
  • 10
    The results are not defined. Which is to say that different implementations are allowed to produce different results. In other words, the results will depend on the compiler, the processor, and possibly the phase of the moon. See [undefined-unspecified-and-implementation-defined-behavior](https://stackoverflow.com/questions/2397984). – user3386109 Jul 23 '19 at 08:48
  • 3
    Unpredictable simple means that the result can be anything. That includes values that you consider meaningful. On another system you may see different results. – Support Ukraine Jul 23 '19 at 08:48
  • 1
    @user3386109 there is no better way to define "unpredictable" than "as the phase of the moon" – Tony Barletta Jul 23 '19 at 08:50
  • 3
    As a realistic example, note that the "overshift" behaviours on x86 are not totally consistent: the SIMD shifts (VPSLLVD etc) make zero in that case, while scalar shifts (SLL etc) take the shift count modulo the operand size. So even on the same platform, using platform semantics, under two different circumstances the result can be different. – harold Jul 23 '19 at 08:53
  • "The right operand should not have a negative value" = What I understand from this sentence is that probably you can't do something like : 2 >> -1 which means bit shifting "-1" times right of the number "2" (WTF ?!). The result of such a shifting can be unpredictable (= something you won't expect) 2 >>1 .I can understand ... – Kobip Jul 23 '19 at 08:57
  • 1
    And by the way there is no such thing as "C/C++". – n. m. could be an AI Jul 23 '19 at 09:08

1 Answers1

4

There is no definition for when the number you're shifting by is negative or exceeds the number of bits that the datatype you're using for your variable can store (in your case 32).

If you got 8 when shifting by -1 that's your compiler's way of doing things, it should not be relied on because it's not defined behaviour nor is it a representative sample, does not mean it right shifted by 1 instead! And other compilers could have completely different results.

For A the result should've been 16384, don't know how you got that 16.

For the shift by more than the number of bits, although not defined by the standard it's possible that you might get a 0 because shifting fills the new bytes with zeroes, however you should not rely on that behaviour because it's not required by the standard.

xception
  • 4,241
  • 1
  • 17
  • 27