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

10

u/TragicCone56813 Jun 15 '26

This is a useful distinction. But it also does use a bit in the information theory way of thinking of a bit which is relevant to the rest of the comment about doubling the positive values.

5

u/sreekotay Jun 15 '26

But this matters because virtually all mathematical operations EXCEPT comparison and negation are the same for sign and unsigned integers

3

u/dmc_2930 Jun 15 '26

And of course bit shifts can differ between signed and unsigned as well.

1

u/sreekotay Jun 15 '26

good point!

2

u/RealisticDuck1957 Jun 15 '26

Which is why twos complement ints is universal for modern architecture.

1

u/markuspeloquin Jun 15 '26

Well, negation is the same for signed vs unsigned. The only exception is -(-2^31)) is -2^31; negation has no effect. I'm not sure if this is UB though.

2

u/sreekotay Jun 16 '26

actually you;re right and I was wrong. it's only compare and bitshift right I think?

1

u/markuspeloquin Jun 16 '26

Well actually we are both wrong because negation makes no sense for signed vs unsigned. I was thinking about how negation works the same for positive vs negative numbers

Edit yes, comparison and right-shift have different instructions for signed vs unsigned.