Signed vs Unsigned Overflow

Discussions center on the differences in overflow behavior between signed and unsigned integers in C/C++, where signed overflow is undefined behavior allowing aggressive compiler optimizations, while unsigned overflow wraps around in a well-defined manner.

πŸ“‰ Falling 0.3x Programming Languages
3,193
Comments
20
Years Active
5
Top Authors
#8135
Topic ID

Activity Over Time

2007
1
2008
8
2009
7
2010
46
2011
86
2012
112
2013
104
2014
167
2015
222
2016
166
2017
134
2018
185
2019
159
2020
232
2021
293
2022
518
2023
319
2024
194
2025
235
2026
5

Keywords

ARM AFAIK UB EOF unsigned signed overflow integer undefined integers undefined behavior ub complement negative

Sample Comments

tekne β€’ Sep 17, 2025 β€’ View on HN

Umm, signed integers are UB on overflow; unsigned is always fine.

marvy β€’ Jun 28, 2014 β€’ View on HN

unsigned overflows are well defined, but signed ones are not

saagarjha β€’ Apr 2, 2021 β€’ View on HN

Signed overflow is undefined behavior.

stopreformation β€’ Dec 14, 2019 β€’ View on HN

It relies on undefined behavior since signed integer overflow is undefined.

norswap β€’ Sep 12, 2013 β€’ View on HN

You're not asking the right question. Most language don't make a distinction between signed and unsigned integer to begin with. And the error being showcased has nothing to do with weird conversions or over/underflows either.

ardel95 β€’ Dec 7, 2022 β€’ View on HN

Tweet said unsigned ints, so don’t think this would work. Even with signed I think this can overflow.

sp1rit β€’ Oct 15, 2024 β€’ View on HN

Yes, as unsigned overflow is fine by default. AFAIK the issue was originally that there were still machines that used ones complement for describing negative integers instead of the now customary twos complement.

nikbackm β€’ Aug 25, 2017 β€’ View on HN

Probably. Signed integer overflow is undefined behaviour in C and C++.

eMSF β€’ Oct 22, 2021 β€’ View on HN

The compiler cannot assume that much, because the argument is a signed integer (negative integers will not overflow and do have well-defined behaviour).

whimsicalism β€’ Jan 20, 2022 β€’ View on HN

The compiler can't warn about something like this? I guess unsigned integer underflow can be the intended behavior often.