我遇到了这段现有的C代码。我正在努力理解它。
我应该将在缓冲区中传递的4字节无符号值(小端格式)读入类型为" long"的变量中。
此代码在64位字长,小端x86机器上运行-其中sizeof(long)是8个字节。
我的猜测是该代码也打算在32位x86机器上运行-因此,为了存储来自四字节输入数据的值,使用了long类型的变量而不是int。
我有一些疑问,并在代码中添加了注释以表达我的理解或不理解的内容:-)
请在这种情况下回答以下问题
void read_Value_From_Four_Byte_Buff( char*input)
{
/* use long so on 32 bit machine, can still accommodate 4 bytes */
long intValueOfInput;
/* Bitwise and of input buffer's byte 0 with 0xFF gives MSB or LSB ?*/
/* This code seems to assume that assignment will store in rightmost byte - is that true on a x86 machine ?*/
intValueOfInput = 0xFF & input[0];
/*left shift byte-1 eight times, bitwise"or" places in 2nd byte frm right*/
intValueOfInput |= ((0xFF & input[1]) << 8);
/* similar left shift in mult. of 8 and bitwise"or