欢迎来到 嗅灵易学

零基础也能上手的脚本技术课,一对一答疑带你入门

C语言一题

C语言一题



为了方便大家辨别,我人肉OCR一下。
2.71:
You just started working for a company that is implementing a set of procedures
to operatate on a data structure where 4 signed bytes are packed into a 32-bit 
unsigned. Bytes within the word are numbered from 0 (least significant) to 3 
(most significant). You have been assigned the task of implementing a function 
for a machine using two's-complement arithmetic and arithemetic right shifts with 
following prototype:
/* Declaration of data type where 4 bytes are packed
   into an unsigned */
typedef unsigned packet_t;
/* Extract byte from word. Return as signed integer */
int xbyte(packet_t word, int bytenum);
That is, the function will extract the designed byte and sign extend it to be a 
32-bit int.
    Your predecessor (who was fired for incompetence) wrote following cod
/* Failed attempt at xbyte */
int xbyte(packet_t word, int bytenum)
{
    return (word >> (bytenum << 3)) & 0xFF;
}
  A. What is wroing with this code?
  B. Give a correct implementation of the function that uses only left and righ
     shifts, along with one subtraction.

注意:上传附件及图片大小不得大于30M。

⚠️ 版权声明:
本博客所有内容(含教程、源码、工具)仅供个人技术学习与研究交流使用,严禁商用、倒卖、二次分发及非法用途。
未经作者书面授权,任何组织或个人不得转载、复制或用于其他平台,违者将追究相关责任。

0 0 0 举报
复制成功