Computer Systems: A Programmer's Perspective (3rd Edition)
Computer Systems: A Programmer's Perspective (3rd Edition)
3rd Edition
ISBN: 9780134092997
Author: Bryant
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 2, Problem 2.71HW

You just started working for a company that is implementing a set of procedures to operate 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 arithmetic right shifts with the following prototype:

/* 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);

That is, the function will extract the designated byte and sign extend it to be a 32-bit int.

Your predecessor (who was fired for incompetence) wrote the following code:

/* Failed attempt at xbyte */

int xbyte (packed_t word, int bytenum)

{

return (word >>(bytenum <<3)) & 0xFF;

}

  1. A.    What is wrong with this code?
  2. B. Give a correct implementation of the function that uses only left and right shifts, along with one subtraction.
Blurred answer
Students have asked these similar questions
BELOW MCQ GIVEN ANSWER CAN BE MORE THAN ONE OPTION. PLEASE PROVIDE CORRECT ANSWERS. ------------------- Q3 :- The ANSI C function below causes the program in which it runs to malfunction . Which of the following connection will help  to perform function successfully ?   /* Return a count of all the bits set in bytes */   int bitcount (unsigned char c) { unsigned char x , b , count =0; for (b = 0 , x= 1 ; !(x & 0x100); ++b, x <<=1) { if (x | c ) ++count; } return count; }   A) change the type of x to a type larger than unsigned char.   B) remove the extraneous variable b.   C) change the return type to unsigned char .   D) change if ( x | c) to if ( x & c) inside the for loop.   E) change ! (c & 0x100) to ( x < 0x100) inside the for loop.
Suppose we number the bytes in a w-bit word from 0 (least significant) to w/8 – 1 (most significant). Write code for the following C function, which will return an unsigned value in which byte i of argument x has been replaced by byte b: unsigned replace_byte (unsigned x, int i, unsigned char b); Here are some examples showing how the function should work: replace_byte(0x12345678, 2, 0xAB) --> 0x12AB5678 replace_byte(0x12345678, 0, 0xAB) --> 0x123456AB Bit-Level Integer Coding Rules In several of the following problems, we will artificially restrict what programming constructs you can use to help you gain a better understanding of the bit-level, logic, and arithmetic operations of C. In answering these problems, your code must follow these rules: Allowed operations All bit-level and logic operations. Left and right shifts, but only with shift amounts between 0 and w – 1. Addition and subtraction. Equality (==) and inequality (!=) tests. (Some of the problems do not allow…
Write a function to convert a string, such as: "-13.232e-5" into a double-precision floatingpoint number. The address of the string should be passed in register $a0. The function shouldreturn the double-precision floating-point number in $f0. Conversion should terminate if theend of the string is reached (NULL byte), or an invalid character is encountered, such as aspace, comma,

Chapter 2 Solutions

Computer Systems: A Programmer's Perspective (3rd Edition)

Ch. 2.1 - Prob. 2.11PPCh. 2.1 - Prob. 2.12PPCh. 2.1 - Prob. 2.13PPCh. 2.1 - Prob. 2.14PPCh. 2.1 - Prob. 2.15PPCh. 2.1 - Prob. 2.16PPCh. 2.2 - Prob. 2.17PPCh. 2.2 - Practice Problem 2.18 (solution page 149) In...Ch. 2.2 - Prob. 2.19PPCh. 2.2 - Prob. 2.20PPCh. 2.2 - Prob. 2.21PPCh. 2.2 - Prob. 2.22PPCh. 2.2 - Prob. 2.23PPCh. 2.2 - Prob. 2.24PPCh. 2.2 - Prob. 2.25PPCh. 2.2 - Practice Problem 2.26 (solution page 151) You are...Ch. 2.3 - Prob. 2.27PPCh. 2.3 - Prob. 2.28PPCh. 2.3 - Prob. 2.29PPCh. 2.3 - Practice Problem 2.30 (solution page 153) Write a...Ch. 2.3 - Prob. 2.31PPCh. 2.3 - Practice Problem 2.32 (solution page 153) You are...Ch. 2.3 - Prob. 2.33PPCh. 2.3 - Prob. 2.34PPCh. 2.3 - Practice Problem 2.35 (solution page 154) You are...Ch. 2.3 - Prob. 2.36PPCh. 2.3 - Practice Problem 2.37 solution page 155 You are...Ch. 2.3 - Prob. 2.38PPCh. 2.3 - Prob. 2.39PPCh. 2.3 - Practice Problem 2.40 (solution page 156) For each...Ch. 2.3 - Prob. 2.41PPCh. 2.3 - Practice Problem 2.42 (solution page 156) Write a...Ch. 2.3 - Practice Problem 2.43 (solution page 157) In the...Ch. 2.3 - Prob. 2.44PPCh. 2.4 - Prob. 2.45PPCh. 2.4 - Prob. 2.46PPCh. 2.4 - Prob. 2.47PPCh. 2.4 - Prob. 2.48PPCh. 2.4 - Prob. 2.49PPCh. 2.4 - Prob. 2.50PPCh. 2.4 - Prob. 2.51PPCh. 2.4 - Prob. 2.52PPCh. 2.4 - Practice Problem 2.53 (solution page 160) Fill in...Ch. 2.4 - Practice Problem 2.54 (solution page 160) Assume...Ch. 2 - Compile and run the sample code that uses...Ch. 2 - Try running the code for show_bytes for different...Ch. 2 - Prob. 2.57HWCh. 2 - Write a procedure is_little_endian that will...Ch. 2 - Prob. 2.59HWCh. 2 - Prob. 2.60HWCh. 2 - Prob. 2.61HWCh. 2 - Write a function int_shifts_are_arithmetic() that...Ch. 2 - Fill in code for the following C functions....Ch. 2 - Write code to implement the following function: /...Ch. 2 - Write code to implement the following function: /...Ch. 2 - Write code to implement the following function: / ...Ch. 2 - You are given the task of writing a procedure...Ch. 2 - Prob. 2.68HWCh. 2 - Write code for a function with the following...Ch. 2 - Write code for the function with the following...Ch. 2 - You just started working for a company that is...Ch. 2 - You are given the task of writing a function that...Ch. 2 - Write code for a function with the following...Ch. 2 - Write a function with the following prototype: /...Ch. 2 - Prob. 2.75HWCh. 2 - The library function calloc has the following...Ch. 2 - Prob. 2.77HWCh. 2 - Write code for a function with the following...Ch. 2 - Prob. 2.79HWCh. 2 - Write code for a function threefourths that, for...Ch. 2 - Prob. 2.81HWCh. 2 - Prob. 2.82HWCh. 2 - Prob. 2.83HWCh. 2 - Prob. 2.84HWCh. 2 - Prob. 2.85HWCh. 2 - Intel-compatible processors also support an...Ch. 2 - Prob. 2.87HWCh. 2 - Prob. 2.88HWCh. 2 - We are running programs on a machine where values...Ch. 2 - You have been assigned the task of writing a C...Ch. 2 - Prob. 2.91HWCh. 2 - Prob. 2.92HWCh. 2 - following the bit-level floating-point coding...Ch. 2 - Following the bit-level floating-point coding...Ch. 2 - Following the bit-level floating-point coding...Ch. 2 - Following the bit-level floating-point coding...Ch. 2 - Prob. 2.97HW

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
CPP Function Parameters | Returning Values from Functions | C++ Video Tutorial; Author: LearningLad;https://www.youtube.com/watch?v=WqukJuBnLQU;License: Standard YouTube License, CC-BY