Chris Low-level systems developer
← Back to all posts

The Shift That Needs Thirty-Three Bits

ISA

No instruction-set claim is made. The 33-bit verdict follows from C's type rules for the shift operator: the literal 1 has type int, int is 32 bits, and a signed shift is defined only when the value is representable in the promoted type. The compiler's diagnostic is emitted before any target is chosen, and the verdict holds with the CPU swapped.

MICROARCH

No claim depends on how any core implements anything. No cache, pipeline, or timing behavior is asserted or measured; the evidence is the compiler's diagnostic text and the defined value of the unsigned form, both produced before any hardware runs.

FIRMWARE

No boot code, machine-mode service, or privilege handoff takes part. The check is a host-side compile of two constant expressions, and the argument never enters firmware.

KERNEL

No kernel is involved at any step. The signed form was compiled as a userspace program on the build VM, and the comparison is between two expressions' definedness, read off the compiler's output rather than from any system call.

ALGORITHM

The claim is a language contract about representability, not a computation. Nothing is proved against an independent oracle, and no portable mechanism is under test.

A peripheral register is 32 bits wide, and the bit you want is bit 31: the enable, the top flag, the master interrupt. My UART driver plays this game lower down: src/uart.c defines LSR_THRE as (1 << 5), one bit up from zero. Bit 31 looks like the same trick, one shift higher. [1]

It is not the same trick. The literal 1 has type int, and int is 32 bits here. Shifting a 1 into the sign position asks for a value that needs 33 bits to represent. GCC 13.3.0 says it out loud with -Wshift-overflow: "result of '1 << 31' requires 33 bits to represent, but 'int' only has 32 bits". The language reference is blunter: for signed left operands the result is defined only when it is representable in the promoted type, otherwise the behavior is undefined. The compiler is allowed to assume it never happens. [2]

On this machine it produced 0x80000000 anyway, which is exactly why the bug survives for years. It works on every compiler you try, right up until the optimization pass that folds it differently, and then your enable bit is a coin flip. Undefined does not mean broken today; it means no promise about tomorrow.

The fix is one character: 1u. Unsigned int has the same 32 bits, but its top bit is a value bit, not a sign bit, and an unsigned left shift is defined as the value reduced modulo 2^32. So 1u << 31 is exactly 0x80000000, on every compiler, forever. The habit: every bit mask in a driver is unsigned. (1u << 31), (1u << 5), all of them. [2]

#define ENABLE_BIT (1u << 31)  /* defined: exactly 0x80000000 */

/* not (1 << 31): that value needs 33 bits in a 32-bit int,
   so the standard calls it undefined */
Blueprint-style diagram of a 32-bit register row: bit 31 is lit amber holding the shifted 1, and a dashed red 33rd cell hangs off the left edge labeled needs 33 bits. Below, the two verdicts: 1 shifted left by 31 is undefined, 1u shifted left by 31 is defined as exactly 0x80000000.
One bit past the edge. (1 << 31) asks for 33 bits in a 32-bit int, so it is undefined; (1u << 31) is exactly 0x80000000, defined.
Diagram: hand-built by Chris Dillinger for this post

Sources

  1. 01
    uart.c: the polled UART driverChris Dillinger

    The register-mask idiom the post starts from: LSR_THRE is defined with a left shift by 5, safe inside a 32-bit int. The same idiom with a shift of 31 is the post's subject.

  2. 02
    Arithmetic operators (C)cppreference.com

    States the rule the post rests on: a signed left shift is defined only when the result is representable in the promoted type of the left operand, otherwise the behavior is undefined; unsigned shifts are reduced modulo 2 to the n.

Related work: bare-metal driver bring-up, fixed price. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app