Assignment
Assignment
QUESTION : 1 >> What is the Size of a.out file for “Hello world” Program ?
The size of a.out file depends on the File Contents and the “Type of Optimization” used while
Compiling the program on GCC Compiler. The Level of Optimization can be passed to GCC using
“-O” tag.
The File sizes and Execution times for various Optimization parameters are given below.
• Time for compilation is taken from ‘real time’ obtained by using “time gcc <filename>”
Since this being a Very small problem with no calculations or loops invloved , The Size of the
a.out file in most of the Optimisations remains constant except for the “-Ofast” tag.
2
QUESTION 2.62 >> Write a function int_shifts_are_arithmetic() that yields 1 when run on a machine that
uses arithmetic right shifts for int’s, and 0 otherwise. Your code should work on a machine with any word
size. Test your code on several machines.
FUNCTION PROGRAM:
int int_shifts_are_arithmetic()
{
int arbitary_negative_number = -1; /* Any Negative Number */
/* Right Shifts the Arbitary Negative number to get the added bit as part (0 or 1, to the LSB
position */
QUESTION 2.69 >> Write code for a function with the following prototype: Mask with least signficant n
bits set to 1 Examples: n = 6 --> 0x2F, n = 17 --> 0x1FFFF .Assume 1 <= n <= w . int lower_one_mask(int
n);
FUNCTION PROGRAM :
int lower_one_mask(int n)
{
/* To get the output as ‘11111....10’ ( Equivalent of –2)in binary, This will eliminate the need to
Shift the bits by w characters when condition becomes n=w */
/* This creates a complementary number which will have o's in places of 1's of the required
answer */
number = ~number;
return number;
}
QUESTION : 2.70 >> Write code for the function with the following prototype: Return 1 when x can be
represented as an n-bit, 2’s complement number; 0 otherwise Assume 1 <= n <= w .int fits_bits(int x, int
n);
PROGRAM:
n = n-1 ; /* to compensate for the Sign Bit that is present, mostly for negative integers */
/* If the integer can be Fit into n bits, It will convert x into 0....0 ( for +ve integer) or 1....11 ( for -ve
integer) */
x = x >> n-1;
/* Converts 0...0 or 1....1 into '0.....1') , if the given integer fits inside n bits*/
QUESTION : 2.82 >> Consider numbers having a binary representation consisting of an infinite string of
the form 0.y y y y y y . . ., where y is a k-bit sequence. For example, the binary representation of 1 3 is
0.01010101 . . . (y = 01), while the representation of 5 1 is 0.001100110011 . . . (y = 0011).
A. Let Y = B2U k (y), that is, 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 the effect of shifting the binary point k positions to the right.
Y = B2U(y) So Y = B2U(0.yyyyyyyyyy)
Right Shifting the binary Decimal place by k positions is equivalent to multiplying the decimal
number by 2 raised to the power of K (2k )
Y10 = (y)2 / ( 2k -1 )
B : B.What is the numeric value of the string for the following values of y?
Y = 101 /( 23 - 1 ) = 5/7
Y = 6/( 24 - 1 ) = 6/15
Y = 19/( 26 - 1 ) = 19/63
5
QUESTION : 2.89 >> You have been assigned the task of writing a C function to compute a floating- point
representation of 2x . You decide that the best way to do this is to directly construct the IEEE single-
precision representation of the result. When x is too small, your routine will return 0.0. When x is too large,
it will return +∞. Fill in the blank portions of the code that follows to compute the correct result. Assume
the function u2f returns a floating-point value having an identical bit representation as its unsigned
argument.
float fpwr2(int x)
{
/* Result exponent and fraction */
unsigned u;
if (x < -149 )
{
/* Too small. Return 0.0 For values greater than 2^-149 */
exp = 0;
frac = 0;
}
else if (x < -127)
{
/* Denormalized result for values including and between (2^-126 and 2^-149)*/
exp = 0;
{
6
/* Normalized result for values including and between ( 2^-127 and 2^127 ) */
exp = x + 127;
frac = 0;
}
else
{
/* Too big. Return +oo */
frac = 0;
}
/* Pack exp and frac into 32 bits */
QUESTION : 2.90 >>Around 250 B.C., the Greek mathematician Archimedes proved that < π <71 7Had he
had access to a computer and the standard library <math.h>, he would have been able to determine that
the single-precision floating-point approximation of π has the hexadecimal representation 0x40490FDB.
Of course, all of these are just approximations, since π is not rational.
0x40490FDB
HEXA TO BINARY :
Hexa Decimal : 4 0 9 9 0 F D B
Equivalent Binary : 0100 0000 0100 1001 0000 1111 1101 1011
= 1.10010010000111111011011 * 21
22/7 = 3.14285714286
Decimal 3 = (11)2
.
.
C. At what bit position (relative to the binary point) do these two approximations to π diverge?
Binary Fraction of Pi : (1 1 . 0 0 1 0 0 1 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 1 )2
They Both Start to Diverge at the 9th Bit in the binary Fraction part