逻辑右移就是不考虑符号位,右移一位,左边补零即可。
算术右移需要考虑符号位,右移一位,若符号位为1,就在左边补1,;否则,就补0。
没有特别说明,就是逻辑移动
左移:一般都是逻辑左移
右移:有符号数算术右移,无符号数逻辑右移
#include<stdio.h>
#include<string.h>
int fun1(unsigned int word)
{
return (int)((word<<24)>>24); //先逻辑左移,再逻辑右移
}
int fun2(unsigned int word)
{
return ((int)word <<24 )>>24; //先逻辑左移 ,再算术右移
}
int main()
{
unsigned int word1 = 0x00000076; //0x00000076 0x00000076
unsigned int word2 = 0x87654321;//0x00000021 0x00000021
unsigned int word3 = 0x000000C9;//0x000000c9 0xFFFFFFc9
unsigned int word4 = 0xDDCBA987; //0x00000087 0xFFFFFF87
//输出:fun1都是 去除前24位
//fun2:有符号数0xFFFFFF
printf("%x\n",fun1(word4));
printf("%x\n",fun2(word4));
return 0;
}