Вывод значения -1U

Кто может объяснить почему выводит 4294967295?

#include <iostream>

signed main() {
  std::cout << -1u << std::endl;
}

Ответы (1 шт):

Автор решения: wololo
  1. Целочисленный литерал 1u имеет тип unsigned int.

  2. Операнд унарного минуса подвергается целочисленному продвижению (integral promotion). В данном случае тип значения, к которому будет применён унарный минус останется без изменения — unsigned int.

  3. Для беззнакового значения x результат применения унарного минуса равен 2**n - x, где n — количество бит в продвинутом операнде. Если тип unsigned int — 32-битный, то получим следующий результат:

    2**32 - 1 == 4294967296 - 1 == 4294967295
    

expr.unary.op/8:

The operand of the unary - operator shall have arithmetic or unscoped enumeration type and the result is the negative of its operand. Integral promotion is performed on integral or enumeration operands. 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. The type of the result is the type of the promoted operand.

→ Ссылка