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 ?

+60
c # types integer unsigned signed
Sep 16 '10 at 6:36
source share
4 answers

Primitive data types prefixed with "u" are unsigned versions with the same bit sizes. Effectively this means that they cannot store negative numbers, but, on the other hand, they can store positive numbers twice as much as their signed copies. Signed counterparts do not have the "u" prefix.

Limits for int (32 bit):

 int: –2147483648 to 2147483647 uint: 0 to 4294967295 

And for long (64 bit):

 long: -9223372036854775808 to 9223372036854775807 ulong: 0 to 18446744073709551615 
+117
Sep 16 2018-10-06T00:
source share

uint and ulong are unsigned versions of int and long . This means that they cannot 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 an unsigned literal int in the 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 want CLS compatible .

Read the documentation for more information:

By the way, there are short and ushort and sbyte .

+27
Sep 16 2018-10-06T00:
source share

u means unsigned , so ulong is a large unsigned number. You can store a larger value in ulong than long , but negative numbers are not allowed.

The value of A long is stored in 64-bit form, with its first digit to indicate whether it is a positive / negative number. while ulong also 64-bit, and all 64 bits are for storing the number. therefore, the maximum of ulong is 2 (64) -1, and long is 2 (63) -1.

+9
Sep 16 '10 at 6:40
source share

The difference is that uint and ulong are unsigned data types, i.e. the range is different: they do not take 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 
+6
Sep 16 '10 at 6:40
source share



All Articles