Quiz Entry - updated: 2026.07.07
What happens when you truncate an integer to a smaller type?
The high bits are simply dropped and the remaining low bits are reinterpreted — for unsigned values this is exactly the value mod 2^w (where w is the new, smaller width).
Truncation is the inverse of expansion: instead of adding high bits, you discard them, keeping only the low w bits.
Example: a 32-bit int truncated to a 16-bit short:
0x12345678 -> 0x5678 (keep only the low 16 bits)
= 22136 (as unsigned short)
Rules:
- Unsigned → smaller unsigned: result = original mod 2^w (pure modular reduction).
- Signed → smaller signed: keep the low w bits, then reinterpret them in the new width — so the result's sign comes from the new MSB, not the old one. This is "similar to mod" but the value can flip sign.
- For values small enough to fit in the smaller type, truncation gives the expected (unchanged) result.
int big = 100000; // 0x000186A0
short small = (short)big; // 0x86A0 = -31072 ! (high bits lost, MSB now 1)
Tip: Expansion always preserves the value; truncation does not — it can silently change or even flip the sign once the value no longer fits.