Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help. i understand how to represent numbers in floating point representat

ID: 3585901 • Letter: I

Question

I need help. i understand how to represent numbers in floating point representation after the lecture in class. the homework assigned is quite difficult though. appreciate it thank you!

the code is in C

Question 1: Write down an function named bitwisedFloatCompare(float number1, float number2) that tests whether a floating point number number1 is less than, equal to or greater than another floating point number number2, by simply comparing their floating point representations bitwise from left to right, stopping as soon as the first differing bit is encountered. The fact that this can be done easily is the main motivation for biased exponent notation. The function should return 1 if number1 > number2, return -1 if number2 > number1 and should return 0 if the two numbers are equal. Please note the solution is constrained to be implemented using bitwise comparison of the two numbers. Question 2: Write a function named printFloatRepresentation(float number) that will print the floating point representation of a number using the format given below. (Sign bit) exponent in binary (assumed bit).significand For example if the number passed an argument is 71 your program should print (0) 10000101 (1).00011100000000000000000 Similarly if the number passed to the function as argument is -71 the program should print (1) 10000101 (1).00011100000000000000000 The main function and function skeletons for the two functions are given in the attached C course. Complete the two functions mentioned in the question.

Question 2: Write a function named printFloatRepresentation(float number) that will print the floating point representation of a number using the format given below. (Sign bit) exponent in binary (assumed bit).significand For example if the number passed an argument is 71 your program should print (0) 10000101 (1).00011100000000000000000 Similarly if the number passed to the function as argument is -71 the program should print (1) 10000101 (1).00011100000000000000000 The main function and function skeletons for the two functions are given in the attached C course. Complete the two functions mentioned in the question.

and here the main function that will be used to test functions

Explanation / Answer

/*
* FloatingPointRepresentation.c
Please do not return this file or the main function with your homework
*/
//This will work on little endian machines
#include <stdio.h>

int bitwisedFloatCompare(float number1, float number2)
{
        int *num1 = (int*) &number1;//converting bytes of float to the integer value
        int *num2 = (int*) &number2;
        if((*num1 & *num2) == *num1) return 0; //when equal
        if(*num1 > *num2) return 1; //if number 1 is greater
        if(*num1 < *num2) return -1; //if number 1 is smaller
}

void printFloatRepresentation(float number)
{
        unsigned char *b = (unsigned char*) &number; //getting address of first byte of the float
        unsigned char byte;//char to store intermediate bitwise anding of the number
        int i, j;//for iterations
        printf("Number %f in binary: ", number);//print the number in normal form
        for (i=sizeof(float)-1;i>=0;i--) //iterating bytes of number from highest byte to lowest
                //assuming machine will be little endian
        {
                for (j=7;j>=0;j--)//
                {
                        if(i==sizeof(float)-1 && j==7) printf("(");//print open parenthesis for the MSB sign bit
                        if(i==sizeof(float)-2 && j==7) printf("(1).");//print implicit one with decimal
                        byte = (b[i] >> j) & 1;//store the value of respective bit in the byte variable
                        //it will be either 0 or 1 depending upon that bit in the number
                        printf("%u", byte);//print byte in unsinged int it will be either 1 or 0
                        if(i==sizeof(float)-1 && j==7) printf(")");//print closing parenthesis for MSB sign bit
                }
                printf(" ");//print space after each byte
        }
        printf(" ");//print newline after printing the complete number
}

int main()
{
        float number1;
        float number2;

        int comparison;

        number1=56.01;
        number2=56;

        comparison=bitwisedFloatCompare(number1,number2) ; // Compare two floating point numbers
      
        if (comparison==1)
                printf("%f is greater than %f ",number1,number2);
        else if (comparison==-1)
                printf("%f is greater than %f ",number2,number1);
        else if (comparison==0)
                printf("Number are equal ");
        else
                printf("Error ");

        printFloatRepresentation(number1);

        return 0;
}