Search code examples
c++bit-manipulationbit-shift

What integer type does bitwise shift use as shift size?


I'm trying to find information about shift's size type in C++. For example:

int x = 1;

char char_var = 1;
short short_var = 1;
int int_var = 1;
long long_var = 1;
long long long_long_var = 1;

x = x << char_var;  // works
x = x << short_var; // works
x = x << int_var;   // works
x = x << long_var;   // works
x = x << long_long_var;   // works

So what type does C++ use for the shift size?


Solution

  • It is explained in [expr.shift]/1: (N4860)

    The operands shall be of integral or unscoped enumeration type and integral promotions are performed

    Unlike most other binary operators, the usual arithmetic conversions are not performed. The integral promotions mean that in your examples the operands of type char and short are promoted to int (on normal systems) and the others are unchanged.