Хорошо, это странная проблема, с которой я никогда раньше не сталкивался:
#include <iostream>
int main()
{
char testchar = 255;
std::cout << (unsigned int)testchar << std::endl;
}
Выход:
4294967295
Что, черт возьми? Кажется, что поскольку char "полный", когда он генерирует новый тип unsigned int, результат также "полный". Это не тот результат, который я помню или ожидаю. Я ожидаю, что unsigned int будет иметь значение 0x000000FF. Может ли кто-нибудь объяснить мне, что именно здесь происходит?
Система: Ubuntu 12.04 64-битная
версия г++: 4.6.3
When a signed integer is converted to an unsigned integer with equal or greater size, if the value of the signed integer is nonnegative, its value is unchanged. Otherwise: if the unsigned integer has greater size, the signed integer is first promoted to the signed integer corresponding to the unsigned integer; the value is converted to unsigned by adding to it one greater than the largest number that can be represented in the unsigned integer type.
цитата из flash-gordon.me.uk/ansi.c.txt 05.11.2012unsigned char
, а затем кunsigend int
. 05.11.2012