logical shift right arithmetic right shift

Logical right shift means that the sign bit is not considered, just one bit is shifted to the right, and the left side is filled with zeros.
Arithmetic right shift needs to consider the sign bit, shift right by one, if the sign bit is 1, add 1 to the left; otherwise, add 0.

There is no special explanation, it is a logical move

Left shift: generally logical left shift

Shift right: Arithmetic right shift for signed numbers, logical shift right for unsigned numbers

#include<stdio.h>
#include<string.h>


int fun1(unsigned int word)
{
return (int)((word<<24)>>24);    //First move left, then move right
}
int fun2(unsigned int word)
{
return ((int)word <<24 )>>24;     //first logical left shift, then arithmetic right shift
}


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

//Output: fun1 is to remove the first 24 bits

//fun2: signed number 0xFFFFFF


printf("%x\n",fun1(word4));
printf("%x\n",fun2(word4));


return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324842601&siteId=291194637