Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference of using int and uint and when to use

What is the difference between using int and uint? All the examples I have seen so far are using int for integers. Any advantage of using uint? Thanks.

like image 903
ThickBook Avatar asked Jun 18 '10 09:06

ThickBook


People also ask

What is the difference between int and UINT?

uint means “unsigned integer” while int means “signed integer”. Unsigned integers only contain positive numbers (or zero). In addition there two alias types: byte which is the same as uint8 and rune which is the same as int32 .

When should you use the unsigned int?

Unsigned integers are used when we know that the value that we are storing will always be non-negative (zero or positive). Note: it is almost always the case that you could use a regular integer variable in place of an unsigned integer.

What is the difference between int and unsigned int?

A signed integer is a 32-bit datum that encodes an integer in the range [-2147483648 to 2147483647]. An unsigned integer is a 32-bit datum that encodes a nonnegative integer in the range [0 to 4294967295]. The signed integer is represented in twos complement notation.

Is Uint bigger than int?

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. To write a literal unsigned int in your source code you can use the suffix u or U for example 123U .


1 Answers

uint means unsigned int, you can use it for a 0 .. +4G range
where the normal (signed) int has a -2G .. +2G range.

When to use it? Almost never. It is not a CLS compliant type so you should never use it in the public interface of an assembly. Not all .NET languages can handle it.

Their main use is in P/Invoke to unmanaged code and some rare bitmask situations. In .NET, we do most bitmasking with normal signed ints.

like image 77
Henk Holterman Avatar answered Sep 20 '22 03:09

Henk Holterman