The greatest mistake you can make in life is to be continually fearing you will make one.
— Elbert Hubbard
将一个整数的高地位进行调换的思路,分别取到每一位,然后按指定顺序排列。
使用 C 语言描述的解法有以下几种。
》》》★
#include
int
main(void)
{
// unsigned short [0, 65535]
// 0xABCD = 43981
unsigned short a = 0xABCD;
unsigned short b, c, d;
printf(“Before convert \n0x%X\n”, a);
b = (a << 8) & 0xFF00;
c = (a >> 8) & 0x00FF;
d = c | b;
printf(“After convert \n0x%X\n”, d);
}
使用 C++ 语言描述的解法。
》》》★
#include
#include // I/O 流控制头文件
using namespace std;
int
main(void)
{
// unsighed short int [0, 65535]
unsigned short int a = 0XABCD;
unsigned short int b, c, d;
cout << “Before convert”
<< endl
// 设置为大写显示
<< setiosflags(ios::uppercase)
<< hex // 十六进制方式显示
<< “0X”
<< a
<< endl;
b = (a << 8) & 0Xff00;
c = (a >> 8) & 0X00ff;
d = c | b;
cout << “After convert”
<< endl
<< hex // 十六进制方式显示
<< “0X”
<< d
<< endl;
}