0

Since the literal 0xffffffff needs 32 digits, it can be represented as an unsigned int but not as a signed int, and is of type unsigned int. But what happens with the negative of an unsigned integer?

#include <iostream>
#include <limits>

int main()
{
  int N[] = {0,0,0};

  if ( std::numeric_limits<long int>::digits==63 and
    std::numeric_limits<int>::digits==31 and
    std::numeric_limits<unsigned int>::digits==32 )
  {
    for (long int i = -0xffffffff; i ; --i)
    {
      N[i] = 1;
    }
  }
  else
  {  
    N[1]=1;
  }

  std::cout << N[0] <<N [1] << N[2];
}

output : 010

1 Answers1

3

There is no such thing as a negative unsigned integer, by definition.

When you go beyond the lower bounds of an unsigned integer, the value "wraps around" starting from the highest possible value. (The same occurs vice versa).

This mechanism is also triggered when converting a negative "signed" value to an unsigned one.

So, the signed value -1 is converted to the unsigned value $maximumUnsignedValue. Similarly, the signed value -$maximumSignedValue is converted to the unsigned value $maximumUnsignedValue - $maximumSignedValue + 1.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 2
    According §5.3.1 in the standard: "The negative of an unsigned quantity is computed by subtracting its value from 2^n , where n is the number of bits in the promoted operand." then Here n is 32, and we get 2^32 - 0xffffffff = 4294967296 - 4294967295 = 1 if im write or i dont understand this Article – tohidprogram May 16 '16 at 11:45