Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Thanks in advance to whoever does this. Write a C program to implement a way of building float values by inputting an appropriate bit-pattern representing

Thanks in advance to whoever does this.

"Write a C program to implement a way of building float values by inputting an appropriate bit-pattern representing the number according to the IEEE standard.

Complete skeleton program (see below) that defines a union type consisting of a 32-bit quantity that can be interpreted as either an unsigned int, an unsigned int that has been partitioned into three bit-fields, or as a float value. The program reads three bit-strings from the command-line, corresponding to the (sign,exp,frac) components of a floating-point number, stores these values in the union, then displays the bit-strings from the union (for debugging), and finally displays the contents of the union as a float value.

What you need to do is

- devise an appropriate bit-field representation for floats that will allow you to easily access to components

- complete the getBits() function, which takes bit-strings from the command-line and stores them in the appropriate components in a Union32 object

- complete the showBits() function, which converts the bit-strings for the three components of a floating point value in a Union32 object into a single C string, formatted as in the examples below"

CODE:

#include #include #include #include

typedef uint32_t Word;

struct _float { // define bit_fields for sign, exp and frac // these may need to be defined in a different order // NOTE: the system is little endian

unsigned int sign:1, exp:8, frac:23; }; typedef struct _float Float32;

union _bits32 { float fval; // interpret the bits as a float Word xval; // interpret as a single 32-bit word Float32 bits; // manipulate individual bits }; typedef union _bits32 Union32;

void checkArgs(int, char **); Union32 getBits(char *, char *, char *); char *showBits(Word, char *); int justBits(char *, int);

int main(int argc, char **argv) { union _bits32 u; char out[50];

// here's a hint ... u.bits.sign = u.bits.exp = u.bits.frac = 0;

// check command-line args (all strings of 0/1 // kills program if args are bad checkArgs(argc, argv);

// convert command-line args into components of // a Float32 inside a Union32, and return the union u = getBits(argv[1], argv[2], argv[3]);

printf("bits : %s ", showBits(u.xval,out)); printf("float: %0.10f ", u.fval);

return 0; }

// convert three bit-strings (already checked) // into the components of a struct _float Union32 getBits(char *sign, char *exp, char *frac) { Union32 new;

// this line is just to keep gcc happy // delete it when you have implemented the function new.bits.sign = new.bits.exp = new.bits.frac = 0;

// convert char *sign into a single bit in new.bits // convert char *exp into an 8-bit value in new.bits // convert char *frac into a 23-bit value in new.bits

return new; }

// convert a 32-bit bit-stringin val into a sequence // of '0' and '1' characters in an array buf // assume that buf has size > 32 // return a pointer to buf char *showBits(Word val, char *buf) { // this line is just to keep gcc happy // delete it when you have implemented the function buf[0] = '\0'; return buf; }

// checks command-line args // need at least 3, and all must be strings of 0/1 // never returns if it finds a problem void checkArgs(int argc, char **argv) { if (argc

// check whether a string is all 0/1 and of a given length int justBits(char *str, int len) { if (strlen(str) != len) return 0;

while (*str != '\0') { if (*str != '0' && *str != '1') return 0; str++; } return 1; }

EXPECTED OUTPUT:

image text in transcribed

$ make gcc -Wall -Werror gcc -o maf make_a_float.o $./maf Usage: ./maf Sign Exp Frac $./maf 0 1 1 c -o make a float.o make a float.c $ ./maf 0 1000000001000000000000000000000 bits : 0 10000000 01000000000000000000000 float: 2.5000000000 $ ./maf 1 10000000 01000000000000000000000 bits 1 10000000 01000000000000000000000 float: 2.5000000000 float: 0.0000000000 // not really 0, but very close ./maf 0 01111111 11000000000000000000000 bits: 0 0111111111000000000000000000000 float: 1.7500000000 $/maf 0 10000010 01000000000000000000000 bits: 0 1000001001000000000000000000000 float: 10.0000000000 ./maf 0 01111100 10000000000000000000000 bits 0 01111100 10000000000000000000000 float 0.1875000000 $ ./maf 0 10000011 10101000000000000000000 bits 0 10000011 10101000000000000000000 float: 26.5000000000 $./maf 0 10001111 11111000000000000000000 bits: 0 10001111 11111000000000000000000 float: 129024.0000000000 float: 0.0000254313

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Question Can life insurance be used in a Keogh (HR 10) plan?

Answered: 1 week ago