'What is the difference between “int” and “uint” / “long” and “ulong”?
I know about int
and long
(32-bit and 64-bit numbers), but what are uint
and ulong
?
Solution 1:[1]
The primitive data types prefixed with "u" are unsigned versions with the same bit sizes. Effectively, this means they cannot store negative numbers, but on the other hand they can store positive numbers twice as large as their signed counterparts. The signed counterparts do not have "u" prefixed.
The limits for int (32 bit) are:
int: –2147483648 to 2147483647
uint: 0 to 4294967295
And for long (64 bit):
long: -9223372036854775808 to 9223372036854775807
ulong: 0 to 18446744073709551615
Solution 2:[2]
uint
and ulong
are the unsigned versions of int
and long
. That means they can't be negative. Instead they have a larger maximum value.
Type Min Max CLS-compliant int -2,147,483,648 2,147,483,647 Yes uint 0 4,294,967,295 No long –9,223,372,036,854,775,808 9,223,372,036,854,775,807 Yes ulong 0 18,446,744,073,709,551,615 No
To write a literal unsigned int in your source code you can use the suffix u
or U
for example 123U
.
You should not use uint and ulong in your public interface if you wish to be CLS-Compliant.
Read the documentation for more information:
By the way, there is also short and ushort and byte and sbyte.
Solution 3:[3]
The difference is that the uint
and ulong
are unsigned data types, meaning the range is different: They do not accept negative values:
int range: -2,147,483,648 to 2,147,483,647
uint range: 0 to 4,294,967,295
long range: –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
ulong range: 0 to 18,446,744,073,709,551,615
Solution 4:[4]
u
means unsigned
, so ulong
is a large number without sign. You can store a bigger value in ulong
than long
, but no negative numbers allowed.
A long
value is stored in 64-bit,with its first digit to show if it's a positive/negative number. while ulong
is also 64-bit, with all 64 bit to store the number. so the maximum of ulong is 2(64)-1, while long is 2(63)-1.
Solution 5:[5]
It's been a while since I C++'d but these answers are off a bit.
As far as the size goes, 'int' isn't anything. It's a notional value of a standard integer; assumed to be fast for purposes of things like iteration. It doesn't have a preset size.
So, the answers are correct with respect to the differences between int and uint, but are incorrect when they talk about "how large they are" or what their range is. That size is undefined, or more accurately, it will change with the compiler and platform.
It's never polite to discuss the size of your bits in public.
When you compile a program, int does have a size, as you've taken the abstract C/C++ and turned it into concrete machine code.
So, TODAY, practically speaking with most common compilers, they are correct. But do not assume this.
Specifically: if you're writing a 32 bit program, int will be one thing, 64 bit, it can be different, and 16 bit is different. I've gone through all three and briefly looked at 6502 shudder
A brief google search shows this: https://www.tutorialspoint.com/cprogramming/c_data_types.htm This is also good info: https://docs.oracle.com/cd/E19620-01/805-3024/lp64-1/index.html
use int if you really don't care how large your bits are; it can change.
Use size_t and ssize_t if you want to know how large something is.
If you're reading or writing binary data, don't use int. Use a (usually platform/source dependent) specific keyword. WinSDK has plenty of good, maintainable examples of this. Other platforms do too.
I've spent a LOT of time going through code from people that "SMH" at the idea that this is all just academic/pedantic. These ate the people that write unmaintainable code. Sure, it's easy to use type 'int' and use it without all the extra darn typing. It's a lot of work to figure out what they really meant, and a bit mind-numbing.
It's fragile coding when you mix int and assume sizes.
use int and uint when you just want a fast integer and don't care about the range (other than signed/unsigned).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Julius Depulla |
Solution 2 | Peter Majeed |
Solution 3 | Peter Mortensen |
Solution 4 | Cheng Chen |
Solution 5 |