What happens to the negation of an unsigned integer?

Since the literal 0xffffffff requires 32 digits, it can be represented as unsigned int, but not as a signed int, but it is of type unsigned int. But what happens to the negation 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

0
source share
1 answer

By definition, there is no such thing as a negative unsigned integer.

When you go beyond the lower limit of an unsigned integer, the value "wraps" starting at the maximum possible value. (The same thing happens the other way around).

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

, -1 $maximumUnsignedValue. -$maximumSignedValue $maximumUnsignedValue - $maximumSignedValue + 1.

+2

Source: https://habr.com/ru/post/1653172/


All Articles