首页 > 试题广场 >

你刚刚开始在一家公司工作,他们要实现一组过程来操作一个数据结

[问答题]

你刚刚开始在一家公司工作,他们要实现一组过程来操作一个数据结构,要将4个有符号字节封 装成一个32位unsigned。一个字中的字节从0(最低有效字节)编号到3(最高有效字节)。分配给 你的任务是:为一个使用补码运算和算术右移的机器编写一个具有如下原型的函数:

/* Declaration of data type where 4 bytes are packed into an unsigned */
 typedef unsigned packed_t;
/* Extract byte from word. Return as signed integer */ 
int xbyte(packed_t word, int bytenum);

也就是说,函数会抽取出指定的字节,再把它符号扩展为一个32位int。你的前任(因为水平不够高而被解雇了)编写了下面的代码:

/* Failed attempt at xbyte*/
int xbyte(packed_t word, int bytenum)
{
return (word >> (bytenum << 3))&OxFF;
}

A. 这段代码错在哪里?

B. 给出函数的正确实现,只能使用左右移位和一个减法。

这道题你会答吗?花几分钟告诉大家答案吧!