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

9

u/mackinator3 Jun 15 '26 edited Jun 15 '26

Signed integer use a bit to determine if its negative or positive. Unsigned doesn't, this limits the numbers to positive, but doubles the amount of numbers it can represent. 

Edit: thanks for the info, I didn't know that extra stuff about it.

18

u/dmc_2930 Jun 15 '26

It’s not a single bit in most systems. It’s twos complement. For a negative value, take the positive one, flip all the bits, and add one. For example 0b11111111 is -1.

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.

8

u/KozureOkami Jun 15 '26

With C23 it’s mandated to be two’s complement. Not that that matters in practice, C standards don’t exactly get rapidly adopted.

7

u/sreekotay Jun 15 '26

In this case, the standard reflects the operating reality of the last 30 years though

→ More replies (4)

6

u/MCLMelonFarmer Jun 15 '26

That post just said that you can look at one bit to determine if the number is negative, and that is true for two's complement representation. It didn't state anything else about how the negative number is represented.

2

u/rasputin1 Jun 15 '26

... that's still using 1 bit for the sign tho? 

1

u/dmc_2930 Jun 15 '26

It’s using lots of bits. I just wanted to state how it actually works, since the statement I replied to could lead to confusion.

2

u/Jumpstart_55 Jun 15 '26

Amusingly the pdp8 add instruction was called TAD (twos complement add)

2

u/Snezzy_9245 Jun 16 '26

And its predecessor the PDP7 had one's complement available, with the confusing positive and negative zero.

1

u/Jumpstart_55 Jun 16 '26

Indeed! I remember using cdc 6500 which had one’s complement integer math.

5

u/EpochVanquisher Jun 15 '26

Additionally, unsigned integers have to wrap around, but with signed integers, you are supposed to avoid overflow. 

→ More replies (12)

5

u/pjl1967 Jun 15 '26

Strictly speaking (i.e., in math), 0 is neither positive nor negative. In twos complement, it's de facto positive; in (rare) ones complement, there is actually positive and negative 0.

1

u/pfp-disciple Jun 15 '26 edited Jun 15 '26

Minor correction: it nearly doubles the highest number it can represent. It's still (often) the same count of numbers. Signed char is (often) -128 to 127, or 256 numbers. Unsigned char is (often) 0-255, or 256 numbers. 

"Doubles the amount of numbers" could correctly be "doubles the amount of positive numbers". 

Edit: lack of coffee, I goofed on CHAR_MIN; it's corrected now 

5

u/goilabat Jun 15 '26

Generally it's -2bits - 1 to 2bits - 1-1 so -128 to 127

As zero is on the positive side

So it's the same amount of numbers you can represent at least with two's complement

1

u/meancoot Jun 15 '26

Signed char is almost always -128 to 127. Signed magnitude and 1’s complement are beyond rare.

→ More replies (1)