c语言的移位操作
http://tieba.baidu.com/f?kz=321200798?? ?
?? ?左移时总是移位和补零。
?? ?右移时无符号数是移位和补零,此时称为逻辑右移;
?? ?而有符号数大多数情况下是移位和补最左边的位(也就是补最高有效位),移几位就补几位,此时称为算术右移。?
?
?? ? ??#include<iostream>
using namespace std;
?
void main() {
//unsigned short temp = 0x8151;
short temp = 0x8151;
cout << temp << endl;
printf("%x\n", temp);
?
unsigned char buf[] = {0, 0};
?
buf[1] = temp & 0x00FF;
temp = temp >> 8;
buf[0] = temp & 0x00FF;
?
//cout << buf[0] << " " << buf[1] << endl;
printf("%x\n", buf[0]);
printf("%x\n", buf[1]);
?
printf("%x\n", temp);
?
//unsigned short temp = 0x8151;
short temp1 = 0x7151;
cout << temp1 << endl;
printf("%x\n", temp1);
?
unsigned char buf1[] = {0, 0};
?
buf1[1] = temp1 & 0x00FF;
temp1 = temp1 >> 8;
buf1[0] = temp & 0x00FF;
?
//cout << buf[0] << " " << buf[1] << endl;
printf("%x\n", buf1[0]);
printf("%x\n", buf1[1]);
?
printf("%x\n", temp1);
?
short temp2 = 0x8151;
unsigned char buf2[] = {0, 0};
*((short *)buf2) = temp2;
printf("%x\n", buf2[0]);
printf("%x\n", buf2[1]);
?
getchar();
?
getchar();
}
?
?
