1

I have the following code in C++

#include <iostream>
using namespace std;
int main(){
    unsigned long long a;
    cin>>a;
    cout<< (1ull<<64ull) << ' ' << (1ull<<a) << endl;
}

now inputting 64, the output is

0 1

In short, the compiler seems to use a circular shift at runtime. But from my reading of the relevant cppreference page, I think it's supposed to be normal modular arithmetic, so the bit should just disappear as in the compile-time version. I tested this on GCC 11, GCC 12 and clang 14 so it's extra unlikely to be a compiler bug. So what am I missing here?

acupoftea
  • 181
  • 2
  • 11

1 Answers1

1

from the quoted cppreference page

In any case, if the value of the right operand is negative or is greater or equal to the number of bits in the promoted left operand, the behavior is undefined.

since 64 is the number of bits in unsigned long long on my machine, this is undefined behavior

acupoftea
  • 181
  • 2
  • 11