With 32-bit int in C++20, which of `-1 << 3`, `1u << 32` and `u8 << 8` are well-defined, and what changed since C++17?
answer
- result type is the promoted left operand
- count must be below the width
- two's complement arrived in C++20
- right shift rounds toward negative infinity
basics
~20 sIn C++20 -1 << 3 is -8 and u8 << 8 promotes u8 to int first, so both are defined; 1u << 32 is undefined because the count equals the width. Before C++20, left-shifting a negative value was undefined.
solid answer
~40 sEach shift operand is promoted on its own, and the result has the promoted left operand's type, so a `std::uint8_t` shifted by 8 is an `int` holding the full value. A shift is undefined if the count is negative or at least the width of the promoted left operand, so `1u << 32` on a 32-bit `unsigned` is undefined in every standard; a constant expression rejects it and UBSan reports it. C++20 made signed integers two's complement: `E1 << E2` is now the value congruent to E1 × 2^E2 modulo 2^N, so `-1 << 3` is `-8`, and `>>` on a negative value is an arithmetic shift rounding toward negative infinity. In C++17, left-shifting a negative value was undefined and right-shifting one was implementation-defined.
code
cpp · 19 lines#include <cstdint>
#include <iostream>
#include <type_traits>
int main() {
std::uint8_t b = 0x81;
auto s = b << 1; // b is promoted to int first
static_assert(std::is_same_v<decltype(s), int>);
std::cout << s << '\n'; // 258, no 8-bit wrap
std::uint8_t back = b << 1; // truncated only on the store
std::cout << int(back) << '\n'; // 2
int neg = -1;
std::cout << (neg << 3) << ' ' << (neg >> 1) << '\n'; // C++20: -8 -1
std::uint64_t wide = std::uint64_t{1} << 40; // widen before shifting
std::cout << wide << '\n'; // 1099511627776
}go deeper
Recall that shifting by the width or more, or by a negative count, is undefined, and that small types are promoted to int before shifting.
Explain why the result type is the promoted left operand, and what C++20's two's complement wording made defined for negative values.
Audit parsers for shift counts taken from data, widen before shifting, and use constant expressions and UBSan to catch out-of-range counts.
If the codebase still builds as C++17, weigh a rule of unsigned-only shifts against relying on C++20 semantics, and enforce whichever is chosen.
## What a shift does to its operands The shift operators `<<` and `>>` take two integral (or unscoped enumeration) operands and apply **integral promotion to each operand separately**. Unlike `+` or `&`, they do **not** apply the usual arithmetic conversions: the **type of the result is the promoted left operand**, whatever the right operand's type is. Two consequences: - a small left operand is widened first: with `std::uint8_t b = 0x81`, `b << 1` is the `int` `258`, not an 8-bit wrap to `2`; the truncation happens only if you store the result back into a `std::uint8_t`; - a wide right operand does not widen the result: `1 << 40` shifts an `int`, and `std::uint64_t{1} << 40` is the way to get a 64-bit shift. ## The two rules that never changed The draft's wording is direct: "The behavior is undefined if the right operand is negative, or greater than or equal to the width of the promoted left operand." So, with a 32-bit `unsigned int`: - `1u << 32` and `1u << n` with `n == 32` are **undefined behaviour**, not `0`; - `x >> -1` is undefined behaviour, not a left shift by one; - the classic mask `(1u << bits) - 1` breaks as soon as `bits` reaches the full width. Some CPUs use only the low bits of the shift count, so a test run often "returns" `1u << 0`, but that is one machine's behaviour on a program the standard does not define. In a **constant expression** the same shift is rejected outright: Apple clang 21 reports "shift count 32 >= width of type 'unsigned int' (32 bits)". At run time `-fsanitize=undefined` reports "shift exponent 32 is too large for 32-bit type 'unsigned int'". ## What C++20 changed for signed values C++20 adopted **two's complement** as the only signed integer representation (paper P1236R1, wording for P0907), and the shift wording changed with it: | Expression (32-bit `int`) | C++17 | C++20 and later | |---|---|---| | `-1 << 3` | undefined behaviour | `-8` | | `1 << 31` | 2^31 converted to `int`: an implementation-defined value | `INT_MIN` | | `3 << 31` | undefined behaviour (not representable even as unsigned) | defined, modulo 2^32 | | `-8 >> 1` | implementation-defined | `-4` (rounds toward negative infinity) | The C++20 rule: `E1 << E2` is "the unique value congruent to E1 × 2^E2 modulo 2^N", where N is the width of the result type, and `E1 >> E2` is `E1 / 2^E2` rounded **toward negative infinity**, an arithmetic shift. So `-1 >> 1` is `-1`, not `0` as division would give. Compiled as C++17, `constexpr int v = -1 << 3;` is rejected with "left shift of negative value -1"; as C++20 it is `-8`. ## Writing shifts in a header parser ```cpp std::uint32_t field(std::uint32_t word, unsigned pos, unsigned bits) { // bits == 32 would make 1u << bits undefined std::uint32_t mask = bits >= 32 ? ~0u : (1u << bits) - 1u; return (word >> pos) & mask; } ``` A few habits keep shift code defined on every standard: 1. Shift **unsigned** values when you mean bit patterns; signed shifts are well-defined only since C++20, and many codebases still build as C++17. 2. Widen **before** shifting: `std::uint64_t{hi} << 32`, never `hi << 32` on a 32-bit `hi`. 3. Check or clamp a shift count that comes from data (a length field, a configuration value) before it reaches `<<`. 4. For rotation of an unsigned value use `std::rotl` and `std::rotr` from `<bit>` (C++20): they take an `int` count and reduce it modulo the width, so negative counts and the full width are fine. ## Promotion surprises with unsigned small types Promotion turns `std::uint8_t` and `std::uint16_t` into **signed** `int`, because `int` can hold all their values. That makes an innocent-looking expression signed: - `std::uint16_t w = 0xFFFF; auto r = w << 16;` gives an `int`; in C++20 its value is `-65536` (the bit pattern `0xFFFF0000` read as two's complement), and in C++17 it was an implementation-defined conversion of a value too large for `int`; - `std::uint64_t wide = w << 16;` then converts that negative `int`, so `wide` is `0xFFFFFFFFFFFF0000`, not `0xFFFF0000`; - `std::uint64_t{w} << 16` widens before the shift and gives `0xFFFF0000`. Apple clang 21 printed exactly those three values; a header parser that assembles a 32-bit field from two 16-bit halves hits this the first time the high half has its top bit set. ## Where this leaf stops What happens when signed **arithmetic** overflows (`INT_MAX + 1`) is a different rule, covered with the classic sources of undefined behaviour; C++20's change made signed *shifts* defined, not signed addition.
- Why does `std::uint32_t hi = 5; std::uint64_t v = hi << 32;` not give `5 << 32`?The result type of a shift is the promoted left operand, here a 32-bit `unsigned int`; the 64-bit destination plays no part. A count of 32 equals that width, so the shift is undefined behaviour. Widen first: `std::uint64_t{hi} << 32`.
- Is `-7 >> 1` the same as `-7 / 2` in C++20?No. C++20 defines `E1 >> E2` as `E1 / 2^E2` rounded toward negative infinity, so `-7 >> 1` is `-4`, while integer division truncates toward zero and `-7 / 2` is `-3`. Code that replaces division by a shift on signed values changes results for negative inputs.
- 1u << 32 is 0 because every bit is shifted out.
- Shifting a std::uint8_t left by 8 wraps around inside 8 bits.
- Assigning to a uint64_t makes hi << 32 a 64-bit shift.
- Left-shifting a negative int is still undefined behaviour in C++20.
- A negative shift count shifts in the other direction.
Assumes C++20 or later; before C++20 left-shifting a negative value was undefined and right-shifting one was implementation-defined.