Why is unsigned int 0xFFFFFFFF equal to int -1?

C or C ++ says that the maximum number that is of type size_t (unsigned data type) can be the same as lithium -1 for this data type. e.g. see Invalid value for size_t

Why?

I mean (talking about 32-bit ints) AFAIK the most significant bit contains the character in the signed data type (i.e., bit 0x80000000 to form a negative number). then 1 is 0x00000001 .. 0x7FFFFFFFF is the largest positive number that the int data type can store.

Then the AFAIK binary representation of -1 int should be 0x80000001 (maybe I'm wrong). why / how is this binary value converted to something completely different (0xFFFFFFFF) when casting ints to unsigned ?? or .. how can I generate binary -1 from 0xFFFFFFFF?

I have no doubt that in C: ((unsigned int) -1) == 0xFFFFFFFF or ((int) 0xFFFFFFFF) == -1 is true than 1 + 1 == 2, I'm just wondering why.

+22
c ++ c casting binary
Dec 07 '09 at 21:48
source share
6 answers

C and C ++ can work on different architectures and machine types. Therefore, they can have different representations of numbers: two complements, and the complement of Odessa is the most common. In general, you should not rely on a specific view in your program.

For unsigned integer types ( size_t , which is one of them), the C standard (and, it seems to me, standard C ++) defines exact overflow rules. In short, if SIZE_MAX is the maximum value of type size_t , then the expression

(size_t) (SIZE_MAX + 1)

0 guaranteed, and so you can be sure that (size_t) -1 is equal to SIZE_MAX . The same is true for other unsigned types.

Please note that this is true:

  • for all unsigned types,
  • even if the base machine does not represent the number in Two additions. In this case, the compiler must verify that the identifier is true.

In addition, this means that you cannot rely on specific views for signed types.

Edit: to answer some of the comments:

Say we have a piece of code, for example:

 int i = -1; long j = i; 

Assignment j has type conversion. Assuming that int and long are of different sizes (most [all?] Of 64-bit systems), the bit patterns in memory locations for i and j will be different because they have different sizes, the Compiler ensures that the values โ€‹โ€‹of i and j are -1 .

Similarly, when we do:

 size_t s = (size_t) -1 

Type conversion occurs. -1 is of type int . It has a bit pattern, but it does not matter for this example, because when the conversion to size_t occurs due to a throw, the compiler will translate the value according to the rules for the type ( size_t in this case), Thus, even if int and size_t are of different sizes, the standard ensures that the value stored in s above is the maximum value that size_t can take.

If we do this:

 long j = LONG_MAX; int i = j; 

If LONG_MAX greater than INT_MAX , then the value in i is determined by the implementation (C89, section 3.2.1.2).

+46
Dec 07 '09 at 22:01
source share
โ€” -

He named two additions. To make a negative number, invert all the bits, then add 1. So, to convert 1 to -1, invert it to 0xFFFFFFFE, then add 1 to make 0xFFFFFFFF.

Regarding this, Wikipedia says:

A system with two additions has the advantage of not requiring the addition and subtraction scheme to check the operands to determine whether to add or subtract. This property simplifies the implementation of the system and makes it easy to process arithmetic with higher accuracy.

+25
Dec 07 '09 at 21:50
source share

Your first question, why (unsigned)-1 gives the maximum possible (unsigned)-1 value, is accidentally associated with only two additions. Reason -1, being cast to an unsigned type, gives the highest value possible for this type, because the standard says that unsigned types "follow the laws of arithmetic modulo 2 n where n is the number of bits in representing the value of this particular integer size."

Now, for 2 additions, the representation of the maximum possible unsigned value and -1 will turn out to be the same - but even if the hardware uses a different representation (for example, 1 addition or sign / value), the conversion of -1 to unsigned type should still produce the highest possible value for this type.

+7
Dec 07 '09 at 22:03
source share

Two additions are very nice to do the subtraction in the same way as adding :)

     11111110 (254 or -2)
    +00000001 (1)
    ---------
     11111111 (255 or -1)

     11111111 (255 or -1) 
    +00000001 (1)
    ---------
    100000000 (0 + 256)
+3
Dec 07 '09 at 22:23
source share

This is two-component encoding.

The main bonus is that you get the same encoding, whether you use an unsigned or signed int. If you subtract 1 from 0, the whole just turns around. Therefore, 1 is less than 0 - 0xFFFFFFFF.

+1
Dec 07 '09 at 21:49
source share

Since the bit pattern for int -1 is FFFFFFFF in unsigned hexadecimal format. 1111111111111111111111111111111111 binary unsigned. But in int, the first bit means whether it is negative. But in unsigned int, the first bit is just an extra number, because unsigned int cannot be negative. Thus, the extra bit makes unsigned int able to store large numbers. As for unsigned int 1111111111111111111111111111111111 (binary) or FFFFFFFF (hexadecimal) is the largest number that uint can store. Unsigned Ints are not recommended, because if they go negative, they overflow and go to the highest number.

-one
Jul 24 '15 at 11:47
source share



All Articles