为什么在这种情况下 x = 44?
Why does x = 44 in this case?
unsigned char x = 0;
unsigned char y = 150;
x = 2 * y;
cout << "x: " << (int)x << endl; // returns 44
我知道 unsigned char 表示从 0 到 255 的 ASCII 字符,但我不知道为什么 x 打印我们的 44。
谁有我可以遵循的 ASCII 图表?
I understand that unsigned char meaning ASCII characters from 0 to 255 but i dont know why the x is printing our 44.
因为 2*150 是大于 255 的 300。300 超出了您系统上 unsined char 的可表示值。
当算术运算的实际结果超出目标无符号类型的可表示值时,结果将与以可表示值的数量为模的实际结果一致。 44 与 300 模 256 全等。
P.S。对于有符号整数类型,如果算术运算的结果超出可表示值,则程序的行为将是未定义的。不惜一切代价避免。
unsigned char x = 0;
unsigned char y = 150;
x = 2 * y;
cout << "x: " << (int)x << endl; // returns 44
我知道 unsigned char 表示从 0 到 255 的 ASCII 字符,但我不知道为什么 x 打印我们的 44。
谁有我可以遵循的 ASCII 图表?
I understand that unsigned char meaning ASCII characters from 0 to 255 but i dont know why the x is printing our 44.
因为 2*150 是大于 255 的 300。300 超出了您系统上 unsined char 的可表示值。
当算术运算的实际结果超出目标无符号类型的可表示值时,结果将与以可表示值的数量为模的实际结果一致。 44 与 300 模 256 全等。
P.S。对于有符号整数类型,如果算术运算的结果超出可表示值,则程序的行为将是未定义的。不惜一切代价避免。