homework_homework4_solution - Assigned: 10/21/02 Due:...

Assigned: 10/21/02 Due: 10/29/02 1 CprE 211 – Fall 2002 Homework 4 Last Name _________________________ First Name _________________________ Section _____________________ Exercise Completed Completion Points (/10) Correctness Points (/20) Points 1 2 3 4 Subtotal Late Penalty Total Grading Procedure for Homework: Every homework will be graded for completion, i.e., did you complete and submit the homework. Each student will have three to five homeworks graded for correctness, randomly selected after each due date (that is, a subset of homeworks and exercises will be graded in full). This procedure will give credit for doing homework while at the same time reducing the time spent on grading homework by the staff. Homework solutions will be posted, and while graded homework will be returned, you may want to keep a copy of your homework for quick comparison with the solutions. Homework Partner(s ): List first & last name of any collaborator and exercise numbers completed together. Instructor/Student Comments: Pages 15-21 consist of code referenced in the homework exercises. Only hand in code pages for your answer to 4b; otherwise, do not hand in the code pages.

This preview
has intentionally blurred sections.
Sign up to view the full version.

Assigned: 10/21/02 Due: 10/29/02 2 1. Representing and Manipulating Information a. Write a C function is_little_endian that will return 1 when compiled and run on a little-endian machine, and will return 0 when compiled and run on a big-endian machine. This program should run on any machine, regardless of its word size. int is_little_endian () { char * pbynum; short shnum = 0xFF11; // assign any unique byte values // to a multi-byte data type pbynum = &shnum; if (*pbynum == 0xFF) return 0; // BE: big byte first else return 1; // LE: little byte first } b. Write a C expression that will yield a word consisting of the least significant byte of x , and the remaining bytes of y . For operands x = 0x89ABCDEF and y = 0x76543210 , this would give 0x765432EF . Both of these work: (y & 0xFFFFFF00) | (x & 000000FF) (y & 0xFFFFFF00) + (x & 000000FF) c. Using only bit-level and logical operations, write C expressions that yield 1 for the described condition and 0 otherwise. Your code should work on a machine with any word size. Assume x is an integer. i. Any bit of x equals 1. x != 0 ii. Any bit of x equals 0. Both of these work: ~x != 0, x != -1 iii. Any bit in the least significant byte of x equals 1. iv. Any bit in the least significant byte of x equals 0. Both of these work: (x & 0xFF) != 0xFF (~x & 0xFF) != 0

Assigned: 10/21/02 Due: 10/29/02 3 d. You just started working for a company that is implementing a set of procedures to operate on a data structure where four 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

This preview
has intentionally blurred sections.
Sign up to view the full version.