i***h 发帖数: 12655 | 1 下面的程序我期待 c=129
为什么高位会是ff?
#include
using namespace std;
int
main()
{
cout << (unsigned short)(2>>1 | 1<<7) << endl;
char c = 2>>1 | 1<<7;
cout << "c = " << (unsigned short)c << endl;
cout << "c = " << hex << (unsigned short)c << endl;
}
>>> output:
129
c = 65409
c = ff81 |
c*****t 发帖数: 1879 | 2 char is signed by default.
【在 i***h 的大作中提到】 : 下面的程序我期待 c=129 : 为什么高位会是ff? : #include : using namespace std; : int : main() : { : cout << (unsigned short)(2>>1 | 1<<7) << endl; : char c = 2>>1 | 1<<7; : cout << "c = " << (unsigned short)c << endl;
|
i***h 发帖数: 12655 | 3 Thanks!
【在 c*****t 的大作中提到】 : char is signed by default.
|
t****t 发帖数: 6806 | 4 by default, char is signed or unsigned is implementation-defined...i've seen
unsigned "char" as well.
that said, char, signed char, unsigned char are *3* different types. don't
make any assumptions.
【在 c*****t 的大作中提到】 : char is signed by default.
|
c*****t 发帖数: 1879 | 5 true (and compilers usually have options), but in most cases, char
is signed by default.
seen
【在 t****t 的大作中提到】 : by default, char is signed or unsigned is implementation-defined...i've seen : unsigned "char" as well. : that said, char, signed char, unsigned char are *3* different types. don't : make any assumptions.
|
t****t 发帖数: 6806 | 6 hehe, i know char is signed in most cases...but it's not beneficial to have
that knowledge.
【在 c*****t 的大作中提到】 : true (and compilers usually have options), but in most cases, char : is signed by default. : : seen
|
i***h 发帖数: 12655 | 7 one more question about char
how to read numbers into char?
I did:
char s;
int foo;
cin >> foo;
s = foo;
Can this code be shorter? Can I get rid of the temporary foo variable?
thanks |
G*******n 发帖数: 2041 | 8 为什么前面补1,而不是补零?
我试了一下,好像MSB是1,前面就补1; MSB是零,前面就补零, 是不是这样
【在 c*****t 的大作中提到】 : char is signed by default.
|
t****t 发帖数: 6806 | 9 >> of negative number is implementation-defined...
【在 G*******n 的大作中提到】 : 为什么前面补1,而不是补零? : 我试了一下,好像MSB是1,前面就补1; MSB是零,前面就补零, 是不是这样
|
i***h 发帖数: 12655 | 10 他问的好像不是这个
a negative number is represented in computer by setting its MSB to 1.
Its absolute value is ~N+1.
For example, 4-bit number 1000
~1000 = 0111
0111+1 = 1000
So 1000 represents -1000 = -8 (decimal)
To extend to 8-bit, 1000 -> 1111 1000
~(1111 1000) = 0000 0111
+1 => 0000 1000 = 8 (decimal)
That's why if negative number it always add 1s to higher bits.
【在 t****t 的大作中提到】 : >> of negative number is implementation-defined...
|
h*******e 发帖数: 225 | 11 that's signed shift. there are unsigned shifts too.
for example, in x86 asm, SHL/SHR are unsigned, while SAR/SAL are signed.
【在 G*******n 的大作中提到】 : 为什么前面补1,而不是补零? : 我试了一下,好像MSB是1,前面就补1; MSB是零,前面就补零, 是不是这样
|
G*******n 发帖数: 2041 | 12 I see your point.
char c = 2>>1 | 1<<7; //c = 0x81 = -127
unsigned short b = c;
可以看成是分两步完成的: short temp = c //temp = -127 = 0xff81
unsigned short b = temp; //b = 0xff81
【在 i***h 的大作中提到】 : 他问的好像不是这个 : a negative number is represented in computer by setting its MSB to 1. : Its absolute value is ~N+1. : For example, 4-bit number 1000 : ~1000 = 0111 : 0111+1 = 1000 : So 1000 represents -1000 = -8 (decimal) : To extend to 8-bit, 1000 -> 1111 1000 : ~(1111 1000) = 0000 0111 : +1 => 0000 1000 = 8 (decimal)
|
h*******e 发帖数: 225 | 13 不要这样看,因为根本就没有这个temp,b = c的时候实际发生的是带符号扩展(如果
编译器不优化的话)。
【在 G*******n 的大作中提到】 : I see your point. : char c = 2>>1 | 1<<7; //c = 0x81 = -127 : unsigned short b = c; : 可以看成是分两步完成的: short temp = c //temp = -127 = 0xff81 : unsigned short b = temp; //b = 0xff81
|