Question: I need this programming question done. It needs to be written in C. Please comment on the code. Thank you. Write a single program (NOT

I need this programming question done. It needs to be written in C. Please comment on the code. Thank you.

Write a single program (NOT TWO programs), that ask the user for an integer (a long integer)

and displays what that long integer looks like in memory. Then the program ask the user for

a floating point number (double) and displays to the user what that double looks like in memory.

Have the program repeat the above behavior (ask for long, show, ask for double, show) until

user gives a non-numeric input.

Note, you are to get the values that are actually in memory and not calculate them.

Again, do not calculate what the bit string should look like (like in your homework) actually

look at what is in memory. I will reject any program that just calculate the bit string values.

The author suggest that using an union type would be a good idea, this certainly will work but is

not the only way. I prefer using points. (this project has to be done in C - not Java)

My hints, first the int:

In C (and most languages, including Java) we have bitwise operators. These bitwise operators

are vary useful and for this project we will be using the BITWISE_AND(&) and the SHIFT (<<)

Consider the following example that shows bitwise AND and SHIFT:

#include

main() {

unsigned int a = 60; /* 60 = 0011 1100 */

unsigned int b = 13; /* 13 = 0000 1101 */

int c = 0;

c = a & b; /* 12 = 0000 1100 */

printf("c = a & b; - Value of c is %d ", c );

c = a << 2; /* 240 = 1111 0000 */

printf("c = a << 2; - Value of c is %d ", c );

c = a >> 2; /* 15 = 0000 1111 */

printf("a = a >> 2; - Value of c is %d ", c );

}

So what we want to do is look at each bit on the integer and if the bit at position i

is a one print a 1 if the bit is a zero print 0.

Consider the following Java method:

static void showbits(int bits){

for(int i=31; i>=0; i--)

System.out.print( (( (bits >> i) & 1) > 0) ? 1 : 0) ;

System.out.print(" ");

}

or a less concise version

static void showbits(int bits){

for(int i=31; i>=0; i--){

int value = bits >> i;

if ((value & 1) == 1)

System.out.print("1");

else

System.out.print("0");

}

System.out.print(" ");

}

The bits is the formal parameter that is passed in. We use a for loop and walk down

the array from Most Significant Bit (MSB) to the least significant bit (63 to 0). For

each bit we check if it is one or zero. We walk down the array by doing a bitwise

shift (>>) and check each bit with a bitwise AND (&). Easy, no.

So we read in a long int. I recommend reading in the long int value as follows:

long int i;

printf(" Please enter a long integer value: ");

scanf(%li",&i);

we could then test if the highest bit place has a one or a zero

by making a mask that would have a 1 and then 63 zeros

e.g. 10000000000000000000000000000000000000000000000000000000000000

long int mask = (1L << 63);

Note long will be 64 bits so mask and the 1 should also be qualified as a LONG (L)

Then we can do a bitwise AND with our number and test if the result greater than zero.

(Or if you want you could shift the other way and test if result is zero.) If this bit was on

(value 1) print 1 else print 0.

if( (i & mask) > 0)

printf("1");

else

printf(0);

For the floating-point (64-bit double), we want to do the same kind of thing.

Read in a double:

double f;

printf(" Please enter a floating point number: ");

scanf(&lf", &f);

but here we need to trick the compiler into treating the float as though it was an integer

The author's idea is to use an union with int and a char[4]

union longdouble {

unsigned long i;

double d

}ld;

where ld is the union and can be both a 64 bit float and a long integer

This idea works but I think it is simpler to not use a union at all and just trick the compiler into

treating the float is an unsigned long int

Note, you should use the exact same function for printing the bits for the double and the long int

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!