很简单的一段代码,有点搞不懂。帮忙看看谢谢
txing 2005-03-23 10:30:41 #include <iostream>
using namespace std;
void disp_binary(unsigned u); //这里的unsigned u 定义的U是什么类型?是整型还是字符型?还是两者都是?
union swap_bytes{
short int num;
char ch[2];
};
int main()
{
swap_bytes sb;
char temp;
sb.num = 15; //二进制形式 :0000 0000 0000 1111
cout << "Original bytes: "; //这里输出的时候为什么先输出sb.ch[1]后输出sb.ch[0]感觉应该是0在前1在后啊?
disp_binary(sb.ch[1]);
cout << " ";
disp_binary(sb.ch[0]);
cout << "\n\n";
//交换字节
temp=sb.ch[0];
sb.ch[0]=sb.ch[1];
sb.ch[1]=temp;
cout << "Enxchanged bytes: ";
disp_binary(sb.ch[1]);
cout << " ";
disp_binary(sb.ch[0]);
cout << "\n\n";
return 0;
}
//输出字节中的位
void disp_binary(unsigned u)
{
register int t;
for(t=128; t>0; t=t/2)
if(u & t) cout << "1 ";
else cout << "0 ";
}
一共两个问题。在代码中都提了。请大家帮忙谢谢