- 求职 : 机器学习
- 论坛徽章:
- 79
|
这种写法会出问题- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- using namespace std;
- int main ( int argc, char *argv[] )
- {
- char s_c[2]={0};
- unsigned char u_c[2]={0};
-
- int s_i=0;
- unsigned int u_i=0;
- signed short int s_s=0;
- unsigned short int u_s=0;
- s_c[0]=0x2E;
- s_c[1]=0xE0;
- u_c[0]=0x2E;
- u_c[1]=0xE0;
- printf ( "%x %x %x %x\n",s_c[0],s_c[1],u_c[0],u_c[1]);
- printf ( "%x %x %x %x\n",s_c[0]<<8,s_c[1],u_c[0]<<8,u_c[1]);
- printf ( "%x %x\n",s_c[0]<<8+s_c[1],u_c[0]<<8+u_c[1]);
- printf ( "%x %x\n",s_c[0]<<8|s_c[1],u_c[0]<<8|u_c[1]);
- printf ( "%x %x\n",s_c[0]*256+s_c[1],u_c[0]*256+u_c[1]);
- printf ( "%x %x\n",s_c[0]*0x100+s_c[1],u_c[0]*0x100+u_c[1]);
- cout<<endl<<endl;
- cout<<"aaaaaaaaaa"<<endl;
- s_i=u_c[0]<<8+u_c[1];
- u_i=u_c[0]<<8+u_c[1];
- s_s=u_c[0]<<8+u_c[1];
- u_s=u_c[0]<<8+u_c[1];
- cout<<dec<<s_i<<":"<<hex<<"0x"<<s_i<<endl;
- cout<<dec<<u_i<<":"<<hex<<"0x"<<u_i<<endl;
- cout<<dec<<s_s<<":"<<hex<<"0x"<<s_s<<endl;
- cout<<dec<<u_s<<":"<<hex<<"0x"<<u_s<<endl;
- cout<<"bbbbbbbbbb"<<endl;
- s_i=u_c[0]<<8|u_c[1];
- u_i=u_c[0]<<8|u_c[1];
- s_s=u_c[0]<<8|u_c[1];
- u_s=u_c[0]<<8|u_c[1];
- cout<<dec<<s_i<<":"<<hex<<"0x"<<s_i<<endl;
- cout<<dec<<u_i<<":"<<hex<<"0x"<<u_i<<endl;
- cout<<dec<<s_s<<":"<<hex<<"0x"<<s_s<<endl;
- cout<<dec<<u_s<<":"<<hex<<"0x"<<u_s<<endl;
- cout<<"cccccccccc"<<endl;
- s_i=u_c[0]*0x100+u_c[1];
- u_i=u_c[0]*0x100+u_c[1];
- s_s=u_c[0]*0x100+u_c[1];
- u_s=u_c[0]*0x100+u_c[1];
- cout<<dec<<s_i<<":"<<hex<<"0x"<<s_i<<endl;
- cout<<dec<<u_i<<":"<<hex<<"0x"<<u_i<<endl;
- cout<<dec<<s_s<<":"<<hex<<"0x"<<s_s<<endl;
- cout<<dec<<u_s<<":"<<hex<<"0x"<<u_s<<endl;
- return 1;
- }
复制代码 2e ffffffe0 2e e0
2e00 ffffffe0 2e00 e0
2e00 2e00
ffffffe0 2ee0
2de0 2ee0
2de0 2ee0
aaaaaaaaaa
11776:0x2e00
11776:0x2e00
11776:0x2e00
11776:0x2e00
bbbbbbbbbb
12000:0x2ee0
12000:0x2ee0
12000:0x2ee0
12000:0x2ee0
cccccccccc
12000:0x2ee0
12000:0x2ee0
12000:0x2ee0
12000:0x2ee0
红颜色标记的部分不对吧,为什么不等于0x2ee0呢?
u_c[0]<<8+u_c[1];
和
=u_c[0]<<8|u_c[1];
到底有什么差别呢?
回复 4# hellioncu
|
|