r/C_Programming • u/Low_Minimum9920 • Jun 15 '26
Question Question regarding unsigned integers
What's the difference between an unsigned int and a normal integer?
11
Upvotes
r/C_Programming • u/Low_Minimum9920 • Jun 15 '26
What's the difference between an unsigned int and a normal integer?
2
u/CarlRJ Jun 15 '26 edited Jun 15 '26
First off, what you call a "normal" int is a signed int. Unsigned ints are simply integers that are not given a special interpretation to handle signs.
The short version is, signed integers give up roughly half the range that would be available for positive numbers, to store negative numbers, with the "upper half" of the original range being used to represent negative numbers.
Working with 16-bit integers just because fewer digits makes it easier to see...
A 16-bit unsigned int covers the range from 0 to 65,535.
A 16-bit signed int covers the range from -32,768 to 32,767.
Signed ints treat the upper half of the available bits differently (using hex to represent the actual bit pattern in the variable):
0x00000x7fff0x80000xffffThe storage scheme chosen might seem slightly counter intuitive at first, until you realize that, with signed ints, if you have 0 and subtract 1, the bit representation rolls back to 0xffff, which is... -1.
30
u/MyTinyHappyPlace Jun 15 '26
Unsigned integers have no sign, hence the name. That usually saves you a bit and allows for a different range of valid numbers.
Also, overflow of unsigned integers is defined, signed integer overflow is undefined behavior.
11
u/Ironraptor3 Jun 15 '26
I am personally confused at multiple comments at making the same wrong claim: that signed integers have HALF the range of an unsigned integer.
unsigned char range: -128 to 127 char range: 0 to 255
Unless I am mistaken, range is defined as (max - min), which is 255 for both of these. Other comments correctly call out that the range occupies a different set of numbers... I wonder if this is just a hard thing to communicate, or the result of people being "helpful" with AI prompting (though, as LLMs fall into similar communication traps, this could still imply "hard to communicate")
9
u/llynglas Jun 15 '26
-1?
4
u/rb-j Jun 16 '26 edited Jun 16 '26
If N is the number of bits in the word, an unsigned integer, x, has range
and a signed integer, y, has range
Unsigned integers can never be negative. Also comparison operators might have different results. Consider this:
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.
2
u/moocat Jun 15 '26
Because they are only positive, they have a different range. So an 32 bit signed integers can represent any value from -2147483648 to 2147483647 while a 32 bit unsigned integer can hold any value from 0 to 4294967295.
2
u/DawnOnTheEdge Jun 15 '26
A signed integer can represent negative numbers, and an unsigned integer can represent twice as many positive numbers (plus one).
There are also some weird little gotchas about how overflowing an unsigned integer makes it wrap around but overflowing a signed one is undefined behavior, or how constants are signed unless they fit in the range an unsigned but not a signed type can represent, or how operations between two different types convert them to a common type. In practice, these make me avoid mixing them.
1
u/zubergu Jun 15 '26 edited Jun 15 '26
To fully understand that difference and all consequences in C you need something more than to hear about 2's complement, as it has nothing to do with C and you wouldn't ask C-unrelated questions on C_Programming sub, would you?
From raw data point of view - there is none. 16-bit 0xFFFF unsigned and signed look exactly the same. Bunch of bits in a memory or in a register. You could stare at them all day and wouldn't have a clue which is which.
It's interpretation that matters.
At the lowest possible level it's CPU that interpretes your numbers.
If you have two variables, one unsigned and second signed int and you want to compare them, your code will be compiled to assembly and then CPU instructions performing exactly that comparison.
You, declaring your variables as unsigned or signed in your code is actually a hint to the compiler what assembly instructions to generate.
On all modern CPUs there are separate instructions to compare signed numbers and unsigned numbers.
For example RISC-V instruction set has these two instructions: blt, and bltu.
First would be used to compare two values in two registers as they were signed integers while the latter would compare them as they were unsigned.
If you were coding in assembly, you'd have to know that and you'd have to remember which register is for which variable, and what your interpretation of these variables was.
But since you're coding in C, you offload this burden to the compiler. You just tell the compiler: 'that one variable is signed, the other is unsigned, now go and generate me assembly->machine instructions that compares them'.
Compiler will take that into consideration and figure out if it should use singed or unsigned version of instructions from target CPU.
So that's the difference between signed and unsigned integers from C language point of view - these are compiler hints for proper assembly instruction generation.
One more thing about those consequences I mentioned in the beginning: there's this whole mess of implicit integer promotion that you should read about, what variable gets promoted to what type in operations depending on their size and if they're unsigned and signed. It's all result of C allowing various operations that your CPU has no instructions for. In that RISC-V instruction set that I mentioned there is no instruction that compares two numbers of which one is signed and the other unsigned, it's one or the other. Compiler has to figure out how to do on given target CPU and C language standard has the whole chapter dedicated to rules on how to do exactly that.
1
u/____sumit____ Jun 15 '26
i was reading the same topic just now :)
take a look at this page of the book. for 16, 32, and 64 computers
Signed(default) : can be negative
unsigned : only be positive (and 0).
1
u/etaithespeedcuber Jun 15 '26
UB as in, it could either go to -1 or -MAX_INT+1?
1
u/SmokeMuch7356 Jun 15 '26
Unsigned types can only represent non-negative values; in signed types, the uppermost bit is reserved for representing the sign (
0for positive,1for negative), while in unsigned types the uppermost bit is part of the value. Unsigned types represent the same number of values as their signed counterparts, just in different ranges.The behavior on unsigned overflow is well-defined, you just wrap around. The behavior on signed overflow is undefined; there is no guaranteed outcome.
Those are the biggest differences.
1
u/RRumpleTeazzer Jun 15 '26
unsigned integers ate nonnegative, and have defined overflow properties. They suck at math though, where signed integers are much better (even when the result is nonnegative).
1
u/Soccera1 Jun 15 '26
Practically it's that only signed integers can store negative numbers.
1
u/KilroyKSmith Jun 16 '26
Absolutely nothing.
It turns out that the computer doesn’t know the difference between signed and unsigned ints. The math is exactly the same, the arithmetic results are exactly the same ( you get exactly the same bits as a result). The difference between them is how you interpret the bit pattern.
This has been one of the main sources of bugs (and hilarity) that computing refuses to fix. Overflowing/underflowing an int or signed int causes ridiculous results like $4 million dollar utility bills - and more subtle bugs that aren’t seen. Yet still no architectures that I’m aware of provide an exception on underflow/overflow.
1
u/theMountainNautilus Jun 16 '26
This absolutely cannot be a serious post. This is one of the most fundamental concepts in programming. I don't want to shame you for not knowing, we all had to learn it at one point. But this has been explained well thousands of times across a variety of resources. You should practice consulting those resources before asking people to put in work to explain it. Reading documentation is an essential skill to learn as you learn programming.
1
u/jontsii Jun 16 '26
Unsigned intergers can only be positive, and signed (normal) interger can be negative and positive, and an unsigned int it double the range of a signed interger, since the sign bit isn´t used.
1
u/sciencekm Jun 16 '26
Just don't compare them. The results would be not what you would expect.
You would think that -1 integer would be less than 1 unsigned. But try this:
ubuntu@vpso1:~/tmp$ cat test.c
#include <stdio.h>
int main(void) {
unsigned u = 1;
int i = -1;
return puts(i < u ? "less" : "not");
}
ubuntu@vpso1:~/tmp$ gcc -Wall test.c
ubuntu@vpso1:~/tmp$ ./a.out
not
The gcc compiler in this case performed an unsigned comparison, and -1 becomes 0xffffffff unsigned which is not less than 1.
0
u/Daveinatx Jun 15 '26
Both use the exact same number of bits, That said signed integers have half the range since the msb bit used for logical operations. The main difference comes with comparison operations or signed shift right operation that follow.
Processors have a number of status bits. If two large numbers were to wrap around, the carry bit is set.
At this point, it's a good time for you to start playing around to see what happens. Using two complement, look at two 32-bit signed and unsigned numbers, -5, +8, -64.
Play around with different combinations of adding and subtracting them. Next, take a look at their text representation doing the same. Finally, disassembler to step through it. Take a look at their status register flags see how they work with different comparisons. Finally, do some shift operations.
I know this is far more than you for asking but if you understand all of this, you'll have solid understanding.