What is the difference between Int32 and UInt32?

后端 未结 5 1594
一整个雨季
一整个雨季 2021-02-07 10:39

What is the difference between Int32 and UInt32?

If they are the same with capacity range capabilities, the question is for what reason U

5条回答
  •  萌比男神i
    2021-02-07 10:57

    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.

提交回复
热议问题