Why it matters

Two's complement powers all modern signed arithmetic. Understanding shapes overflow behavior.

Advertisement

The architecture

Encode: invert bits, add 1.

Range: -2^(n-1) to 2^(n-1)-1.

Overflow wraps; UB in C for signed.

Two's complement signedEncodeinvert + 1Range asymmetricone more negativeOverflowwraps in HW, UB in CAll modern CPUs; C signed overflow is UB even though HW wraps
Two's complement encoding.
Advertisement

How it works end to end

Modern CPUs use 2's complement.

Sign-extension via arithmetic right shift.

Overflow behavior differs C vs HW.