r/C_Programming • • Jun 15 '26

Question Question regarding unsigned integers

What's the difference between an unsigned int and a normal integer?

11 Upvotes

93 comments sorted by

View all comments

Show parent comment

1

u/sreekotay Jun 15 '26

float nor double uses two's complement.

1

u/dmc_2930 Jun 15 '26

Right? and of course floats and doubles are not integers.

1

u/flatfinger Jun 16 '26

The principle at play is the computation of temporary results which are larger than int. Such permission would allow code generation simplifications such as being able to compute int1*int2+long1 without having to sign-extend the product, or allowing transforms transforms such as x+y>x into y>0, or x*(y*d)/(z*d) for positive d into x*y/z, all while processing uint1=ushort1*ushort2; as the authors of the Standard intended (according to the published Rationale document).

While clang and gcc can be configured to use precise wraparound semantics, compilers for some targets such as the TMS32050 can't. On that platform, computing (long)int1+(long)int2+long1 would be much faster than (int)((unsigned)int1+(unsigned)int2)+long1 and there is no option to process int1+int2+long1 as equivalent to the latter.