What is the difference between “int” and “uint” / “long” and “ulong”?
Asked Answered
K

5

158

I know about int and long (32-bit and 64-bit numbers), but what are uint and ulong?

Kovar answered 16/9, 2010 at 6:36 Comment(0)
B
307

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
Buckish answered 16/9, 2010 at 6:39 Comment(5)
This is quite fun to work out by hand. A 32-bit signed variable uses 1 bit for the sign (positive or negative) so can store values between -2^31 and +2^31 - 1Reverential
when comparing int and uint for usage, which one is feasible?Debacle
What's the c++ equivalent?Courthouse
@JacoPretorius Thats wrong. 8 bit int has a range from –128 to 127. The 9th bit represents 256. So with 8 bits you can represent all values up to 255 (9th val - 1). The range from -128 to 127 has a length of exactly 255. So there is no bit that holds the sign. All values up to 127 are positive. Values above get displayed negative. 255 would be -1. 254 would be -2 and so one way down to 128.Pianoforte
I think it's also worth noting that specifically for int vs uint, the unsigned integer is not CLS-compliant, and it's recommended to use int as often as possible.Unfeeling
U
47

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.

Unnecessary answered 16/9, 2010 at 6:39 Comment(3)
This is interesting - what do you mean about CLS compliant? The link goes to the MSDN documentation for int. If by "CLS" you mean C# language spec then I don't understand - the spec clearly describes both uint and ulong (section 1.3)Buckish
@Isak Savo: It is important to be CLS-compliant if you are writing interface that could be used by other .NET languages than C#.Unnecessary
Curious that you mention short and ushort but leave out byte and sbyte :)Riarial
C
13

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.

Craniology answered 16/9, 2010 at 6:40 Comment(0)
W
12

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
Wesley answered 16/9, 2010 at 6:40 Comment(0)
G
0

Based on the other answers here and a little review you can understand it this way: unsigned is in reference to the assignment of a negative or positive explicit assignment (think the "-" in -1) and the inability to have negative versions of said numbers.

And because of this capacity on the negative end being removed as an option they instead allocated that capacity to the positive end hence the doubling of the positive valuation's maximum value. So instead of the bit range being split along positive and negative valuations, they are instead for ushort, uint, along, etc allocated to the positive end of the valuation.

Grice answered 24/7, 2022 at 6:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.