The real difference between “int” and “unsigned int”

后端 未结 9 542
时光取名叫无心
时光取名叫无心 2020-12-12 16:13

int:

The 32-bit int data type can hold integer values in the range of −2,147,483,648 to 2,147,483,647. You may also refer to this data type as sig

9条回答
  •  离开以前
    2020-12-12 16:47

    The binary representation is the key. An Example: Unsigned int in HEX

     0XFFFFFFF = translates to = 1111 1111 1111 1111 1111 1111 1111 1111 
    

    Which represents 4,294,967,295 in a base-ten positive number. But we also need a way to represent negative numbers. So the brains decided on twos complement. In short, they took the leftmost bit and decided that when it is a 1 (followed by at least one other bit set to one) the number will be negative. And the leftmost bit is set to 0 the number is positive. Now let's look at what happens

    0000 0000 0000 0000 0000 0000 0000 0011 = 3
    

    Adding to the number we finally reach.

    0111 1111 1111 1111 1111 1111 1111 1111 = 2,147,483,645
    

    the highest positive number with a signed integer. Let's add 1 more bit (binary addition carries the overflow to the left, in this case, all bits are set to one, so we land on the leftmost bit)

    1111 1111 1111 1111 1111 1111 1111 1111 = -1
    

    So I guess in short we could say the difference is the one allows for negative numbers the other does not. Because of the sign bit or leftmost bit or most significant bit.

提交回复
热议问题