Вывод значения -1U
Кто может объяснить почему выводит 4294967295?
#include <iostream>
signed main() {
std::cout << -1u << std::endl;
}
Ответы (1 шт):
Целочисленный литерал
1uимеет типunsigned int.Операнд унарного минуса подвергается целочисленному продвижению (integral promotion). В данном случае тип значения, к которому будет применён унарный минус останется без изменения —
unsigned int.Для беззнакового значения
xрезультат применения унарного минуса равен2**n - x, гдеn— количество бит в продвинутом операнде. Если типunsigned int— 32-битный, то получим следующий результат:2**32 - 1 == 4294967296 - 1 == 4294967295
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 from2**n, wherenis the number of bits in the promoted operand. The type of the result is the type of the promoted operand.