What is the difference between Int32 and UInt32?
Asked Answered
F

5

24

What is the difference between Int32 and UInt32?

If they are the same with capacity range capabilities, the question is for what reason UInt32 was created? When should I use UInt32 instead of Int32?

Fabien answered 21/2, 2010 at 20:3 Comment(4)
Do you know the difference between signed and unsigned integers?Hypogeous
@Moron: Yen I know. just thinking accidentally about UInt32 as 'unmanaged int32' instead of 'unsigned int32'. hhaaa!!Fabien
Voting to close as "too localized", as this question is only useful to people who think that UInt32 means "unmanaged int32". I don't think there are any more of those people left.Phonic
@Jhon: I agree with you.Fabien
P
51

UInt32 does not allow for negative numbers. From MSDN:

The UInt32 value type represents unsigned integers with values ranging from 0 to 2 to the power of 32 or 2**32 (which equals to 4,294,967,295).

Plastic answered 21/2, 2010 at 20:4 Comment(1)
(2**32) -1 = 4294967295 as you need to account for the 0 you need to reduce the max value by 1.Thereinafter
T
16

An integer is -2147483648 to 2147483647 and an unsigned integer is 0 to 4294967295.

This article might help you.

Tympan answered 21/2, 2010 at 20:5 Comment(0)
I
11

uint32 is an unsigned integer with 32 bit which means that you can represent 2^32 numbers (0-4294967295).

However, in order to represent negative numbers, one bit of the 32 bits is reserved to indicate positive or negative number. this leaves you with 2^31 possible numbers in the negative and also in the positive. The resulting range is -2147483648 to 2147483647 (positive range includes the value 0, hence only 2147483647). This representation is called int32.

You should choose unsigned for numbers which can't get negative by definition since it offers you a wider range, but you should keep in mind that converting from and to int32 is not possible since int32 can't hold the range of uint32 and vice versa.

Icky answered 21/2, 2010 at 20:47 Comment(0)
I
2

uint32 is unsigned 32-bit integer. It can't be used to represent negative numbers but can hold greater positive numbers.

Isidore answered 21/2, 2010 at 20:5 Comment(0)
W
-4

There is no difference between them.

The difference is how it is represented, such as via printing to terminal.

For example, and sticking with 8 bit values for simplicity:

  • 255 is 0xff, and -1 is also 0xff.
  • Interpret -1 as, "One to the left of zero", whereas +1 is, "One to the right of zero".
  • -1 + -1 = -2, which is, "Two to the left of zero", which is also 0xfe. Well, if we convert that to unsigned math, then, it becomes 0xff + 0xff = 0xfe.

So you see, there is no difference between signed and unsigned, it's only how we represent them in the end that makes the difference, and in this case, the type indicates how it is represented.

Signedness becomes important when the compiler has to cast from a smaller size to a bigger via sign extension. So in this case, it's important to indicate a type so that the compiler can do the right thing.

Wilie answered 28/4, 2020 at 19:29 Comment(3)
"There is no difference between them" Deeply wrong. Signed ints reserve 1 bit for determining if a number is positive or negative. Unsigned ints do not reserve a bit for this, meaning they can only be positive and not negative. This one bit is a massive difference, as it doubles the maximum value of the number at the cost of negative numbers (we work with powers of two, and 31^2 is 4294967295).Ringtail
You're so certain of yourself. At the register level, there is no difference in how unsigned and signed integers are stored. There is no bit flip designated for negative numbers. According to what you proposed, -1 is an unsigned one with its most significant bit set. This is deeply wrong. -1 is zero minus 1, which results in 0xFF in an 8 bit architecture. This way, when we add a positive one to 0xFF, the result is 0. Negative numbers simply map to unsigned numbers under the hood. What's -1 + -2 in 8 bit math? 0xff + 0xfe = 0xfd. This is called 2s compliment arithmetic.Wilie
This answer is so underrated, and it's sad to see people voting it down. I would add however another difference: the compiler will use arithmetic shift on signed types.Quincey

© 2022 - 2024 — McMap. All rights reserved.