CSCI 2122 - Systems Programming - A2


  • 作业标题:CSCI 2122 - Systems Programming - A2
  • 课程名称:Dalhouse University CSCI 2122 Systems Programming
  • 完成周期:2天

1. Introduction

Note: Please provide computations/justifications/explanations for each answer.
Simply stating an answer will earn no credit.

(1) This problem considers numbers having a binary representation consisting of an infinite string of the form: 0:yyyyy : : :, where y is a k−bit sequence. For example, for example y = 01 gives the binary representation of 1 3, while the representation of 15 is given by y = 0011.

(a) [6] Let Y = B2Uk(y), i.e. the number having binary representation y. Give a formula, in terms of Y and k for the value represented by the infinite string. (Hint: Consider shifting the binary point k positions to the right. If S were the value of the original string, what would the value of the shifted string be?)

(b) [2] What is the numeric value of the (infinite) string for the following values of y?

(A) 101 (B) 0110 (C) 0011 (D) 010011

(2) [6] Write a function with the following prototype:

/* Determine whether the arguments can be subtracted w/o overflow */
int tsub ok ( int x, int y );

The function should return 1 if the computation does not overflow.

(3) [6] You are given the task of writing a a function that will copy an integer val into a buffer buf, but should only do so if there is enough space available in buf.

/* Copy integer into buffer if space available */
/* BUGGY CODE */
void copy int ( int val, void *buf, int maxBytes)
if (maxBytes - sizeof(val) >= 0)
memcpy (buf, (void *), &val, sizeof(val));

The code uses the library function memcpy. It illustrates an approach commonly used to copy larger data structures. You test the code and fin that it always copies val to buffer, even when maxBytes is too small. Explain why the conditional test in the code always succeeds. Hint: The sizeof operator returns a value of type size t .

(4) [8]Write code for a function multBy3divBy4, that for argument int x, computes 3x/4, but follows the bit-level integer coding rules (text p.128) like in problem 3 of the assignment 1. (you may assume ints are 32 bits). (Note: The operation in your code 3x is allowed to cause overflow.)

(5) [15 = 0.5/val of A + 1 each for all other blanks] Consider the following 9-bit floating point representations based on the IEEE format. Format A: 1 sign bit; k = 5 exponent bits; n = 3 fractional bits. Format B: 1 sign bit; k = 4 exponent bits; n = 4 fractional bits. In the following table you are given some bit patterns in Format A. Convert them to the closest value in Format B:. If rounding is necessary, round toward +1. Give the values of the numbers in the two formats as fractions: (e.g. 17=64 or 17=26). Show all work.

。。。


文章作者: IT神助攻
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 IT神助攻 !
  目录