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();

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值