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;
}
- A. What is wrong with this code?
- B. Give a correct implementation of the function that uses only left and right shifts, along with one subtraction.
Want to see the full answer?
Check out a sample textbook solutionChapter 2 Solutions
EBK COMPUTER SYSTEMS
Additional Engineering Textbook Solutions
Starting Out with C++: Early Objects (9th Edition)
Thinking Like an Engineer: An Active Learning Approach (4th Edition)
Starting Out with Python (4th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
Concepts Of Programming Languages
Starting Out with C++ from Control Structures to Objects (9th Edition)
- code required in mips programming language a .s or .asm code not a c code. Write a MIPS procedure that takes as its two parameters the starting address of a (zero-terminated) string, and a character c, and removes all instances of the character from the string. Also, write a main program to test your procedure. Your main program should input a string from the user (you can assume that the string will be at most 40 characters, not including the zero byte delimiter), and then prompt for input of a character c. If the character c that the user inputs is the newline character (ascii code 10), your program should terminate. Otherwise, your program should invoke your procedure, output the modified string, and then prompt for input of another character to be removed from the string, continuing in this manner until the input character is the newline characterarrow_forwardIn c++ Now write a program for a double floating pointtype.•What are the number of significant bits that the double’s mantissa can hold, excluding the sign bit.•What is the largest number that a double’s mantissa can hold without roundoff error?•Repeat the program shown on the previous page but set to show where the double’s mantissa starts to exhibit roundoff errors.arrow_forwardSolve the below program in C language. Write a program in C to swap two numbers using function. Test Data :Input 1st number : 2Input 2nd number : 4Expected Output : Before swapping: n1 = 2, n2 = 4 After swapping: n1 = 4, n2 = 2arrow_forward
- in c++ pleasearrow_forwardThis in C language Defining a binary number as Program 1, write the function int binToDec(const int bin[]) to convert an eight-bit unsigned binary number to a nonnegative decimal integer. Do not output the decimal integer in the function. Test your function with interactive input. Defining bAnd, bin1, and bin2 as binary numbers as in Program 1 above, write the void function void binaryAnd(int bAnd[], const int bin1[], const int bin2[]) to compute bAnd as the logical AND of the two binary numbers bin1 and bin2. Do not output the binary number in the function. Test your function with interactive input. Program1 in C: #include <stdio.h>int main(){int binNum[8]; // Array to read binary numberlong dec=0,n=0; // variables used to convert binary to decimalint k=0,l=0;long binary=0;int i=1,j=0,remainder=0; //reading the binary number in to the array binNumprintf("Please Enter the first binary number with each bit seperate by at least one space : \n"); scanf("%d %d %d %d %d %d %d…arrow_forwardWrite a code in sim8085 for the following problem: The pressure of two boilers is monitored and controlled by a microcomputer works based on microprocessor programming. A set of 6 readings of first boiler, recorded by six pressure sensors, which are stored in the memory location starting from 2050H. A corresponding set of 6 reading from the second boiler is stored at the memory location starting from 2060H. Each reading from the first set is expected to be higher than the corresponding position in the second set of readings. Write an 8085 sequence to check whether the first set of reading is higher than the second one or not. If all the readings of first set is higher than the second set, store 00 in the ‘D’ register. If any one of the readings is lower than the corresponding reading of second set, stop the process and store FF in the register ‘D’. Data (H): First set: 78, 89, 6A, 80, 90, 85 Second Set:71, 78, 65, 89, 56, 75arrow_forward
- Please help me write a MIPS program that actually worksarrow_forwardWrite C code that contains a function called triangle_generator that outputs a triangle wave at a specified frequency (in hertz) using a specified sample rate (in hertz), and for a specified time duration (in seconds). These parameters are float type. The output you generate are floating point numbers between -1 and 1, one number per output line. The math trig functions in C use radians while all the specifications here are in hertz (cycles per second). One hertz is 2*Pi radians.arrow_forwardCreate a program in C++ which checks for 1 bit errors in an error detection system which uses a single parity bit (the single parity bit checks the parity of all bits in each code). Each code is 8 bits (7 bits for data and one for parity). The code distance is 2. You program should check for even parity (parity bit is 1 for even parity). Your program should allow the user to input a single unsinged decimal value up to 255, and then output whether there is a 1 bit error or not.arrow_forward
- Using C++, write a program that converts an integer to 32-bit two's complement. Add a space between every 8 bits. Do not use external libraries that provide the calculations automatically.Example outputThis program converts an integer to 32-bit two's complement.Enter an integer: 1999Two's complement: 00000000 00000000 00000111 11001111Enter an integer: -65535Two's complement: 11111111 11111111 00000000 00000001arrow_forwardPYTHON WITHOUT USING DEF FUNCTION Define and implement two functions, to_hex and rgb_to_hex. to_hex should (1) take as its only parameter an integer decimal number and (2) return the hexadecimal representation as a string. (Hint: If you are in CSCI 101, you may want to take a look at your Binary-Octal-Decimal Converter lab. The logic for converting from decimal-hex is similar to decimal-octal and decimal-binary). rgb_to_hex should (1) take as its only parameter a tuple of three elements (R, G, B) and (2) return the corresponding hexadecimal translation as a string. It should call to_hex multiple times. Example Execution #1 >>> rgb_to_hex((255, 167, 89))'FFA759'>>> rgb_to_hex((0, 0, 0))'000000'>>> rgb_to_hex((187, 15, 231))'BB0FE7'arrow_forwardUsing the C Programming language, write two versions of a function that contains a loop. Eachthe function should accept two numbers and calculate the sum of all numbers between the first numberand last number (inclusive of the first and last number). Once each function is writtenand tested for correct output, generate an assembly language version of the function. Write a version of the function using a while loop Write a version of the function using a goto loop Is the assembly language version of each loop function the same or different? (hint: Number of registers used Number of jumps (iterations), Total number of operations).arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education