Question

In: Computer Science

using C , comments will be appreciated. I already posted this before and I am looking...

using C , comments will be appreciated. I already posted this before and I am looking for different answer.

please answer both part of the question.
A) Write down an function named bitwisedFloatCompare(float number1, float number2)

that tests whether a floating point number number1is 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 differingbit 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 notethe solutionis constrainedto be implemented using bitwise comparison of the two numbers.

B) 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).

significandFor example if the number passed an argument is 71 yourprogram 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 forthe twofunctions are given in the attached C course. Complete the two functions mentioned in the question.

/*
* FloatingPointRepresentationFunctions.c
*/


//************************************************************************************/
//
//                                FloatCompare
//
//           Description:Accepts two float numbers and compares them bitwise based
//                   on floating point representations. This function will have
//                    to convert the given numbers into IEEE floating
//                    representation and then do bitwise comparison
//           Preconditions:two input arguments are passed
//           Postconditions:Returns 1,-1 or 0 based on the comparison
//                  1 if number1>number2
//                               -1 if number2>number1
//                                  0 if equal
//           Calls:           N/A
//           Called by:       main
//
//***********************************************************************************/

int bitwisedFloatCompare(float number1,float number2)
{
   // Write the function to compare and return the corresponding value


}

/*
* FloatingPointRepresentation.c
Please do not return this file or the main function with your homework
*/

#include <stdio.h>

int main()
{
   float number1;
        float number2;

   int comparison;

   number1=56;
        number2=12;

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

  

   return 0;
}

Solutions

Expert Solution

A]

   int bitwisedFloatCompare(float number1,float number2){
  
    int arr1[65];
    int arr2[65];
  
    for(int i=0;i<65;i++)arr1[i]=0;
    for(int i=0;i<65;i++)arr2[i]=0;
  
    int a = number1;
    int b = number2;
  
    double remA = number1-a;
    double remB = number2-b;
  
  
    //THE ARRAY STORES IN THE FIRST 32 BITS THE BINARY REPRESENTATION OF THE INTEGRAL PART
    //AND THE NEXT POSITION IS BLANK REPRESENTING THE DECIMAL POINT
    // AND THE NEXT 32 BITS REPRESENT THE FRACTIONAL PART
  
    //BINARY REPRESENTATION OF NUMBER1 STORED IN ARR1
    int j = 31;
    while(a>0 && j>=0){
        arr1[j] = a%2;
        a = a/2;
        j--;
    }
    j = 33;
    int x;
    while(j<65){
        x = remA;
        arr1[j] = x;
        printf("%f\n",remA);
        if(x==1){
            remA = remA-1;
        }
        remA = 2*remA;
        j++;
    }
  
    //BINARY REPRESENTATION OF NUMBER2 STORED IN ARR2
    j = 31;
    while(a>0 && j>=0){
        arr2[j] = b%2;
        b = b/2;
        j--;
    }
    j = 33;
    while(j<65){
        x = remB;
        arr2[j] = x;
        if(x==1){
            remB = remB-1;
        }
        remB = 2*remB;
        j++;
    }
  
    for(int i=0;i<65;i++){
        if(i==32)
        else {
            if(arr1[i]==arr2[i]){
                continue;
            }else{
                if(arr2[i]==1 && arr1[i]==0){
                    return -1;
                }else{
                    return 1;
                }
            }
        }
    }
  
    return 0;
  
}

B]

void printBinary(int n, int i)
{

    // Prints the binary representation
    // of a number n up to i-bits.
    int k;
    for (k = i - 1; k >= 0; k--) {

        if ((n >> k) & 1)
            printf("1");
        else
            printf("0");
    }
}

typedef union {

    float f;
    struct
    {

        // Order is important.
        // Here the members of the union data structure
        // use the same memory (32 bits).
        // The ordering is taken
        // from the LSB to the MSB.
        unsigned int mantissa : 23;
        unsigned int exponent : 8;
        unsigned int sign : 1;

    } raw;
} myfloat;

// Function to convert real value
// to IEEE foating point representation
void printFloatRepresentation(myfloat var)
{

    // Prints the IEEE 754 representation
    // of a float value (32 bits)

    printf("(%d)", var.raw.sign);
    printBinary(var.raw.exponent, 8);
    printf("(1).");
    printBinary(var.raw.mantissa, 23);
    printf("\n");
}


/***** USAGE ********/

// myfloat var;

// // Get the real value
// var.f = -2.25;

// // Get the IEEE floating point representation
// printf("Float representation of %f is : \n",
//       var.f);
// printFloatRepresentation(var);

/***** END   ******/


Related Solutions

I posted this question looking for a new paper not an answer that's already posted on...
I posted this question looking for a new paper not an answer that's already posted on chegg since if someone already turned in that paper it will show as a copied paper... the question is.. write a paper of 300-600 words presenting your analysis of the following topic- Discuss shortages and surpluses and how they re-direct resources
i posted this question before , but the answer was totally wrong . i am developing...
i posted this question before , but the answer was totally wrong . i am developing e-commerce website for my project in my school . this testing is not required to testing by code . and i need to fail the table , and example should be related to e-commere website . hi everyone i need your help , actually i trying to develop e commerce website and i have to document the testing for my system in my project...
BEFORE YOU ANSWER THIS QUESTION, PLEASE READ: I HAVE ALREADY POSTED THIS QUESTION MULTIPLE TIMES, AND...
BEFORE YOU ANSWER THIS QUESTION, PLEASE READ: I HAVE ALREADY POSTED THIS QUESTION MULTIPLE TIMES, AND I NEED HELP WITH THE JOURNAL ENTRIES. PLEASE INCLUDE THEM IN THE ANSWER. THANK YOU Overhead Variances, Four-Variance Analysis, Journal Entries Laughlin, Inc., uses a standard costing system. The predetermined overhead rates are calculated using practical capacity. Practical capacity for a year is defined as 1,000,000 units requiring 200,000 standard direct labor hours. Budgeted overhead for the year is $750,000, of which $300,000 is...
This program needs to be in C++. I have barely gotten anywhere but am already stuck....
This program needs to be in C++. I have barely gotten anywhere but am already stuck. Any help with this one would be greatly appreciated. Write a program that simulates playing a simplified version of the game "Candy Land." In "Candy Land" players take turns drawing cards that have a colored square on them. The player then travels on the board according to the color on the card. The first person to reach the end of the board wins. In...
I know that there are several different answers that has already been posted for this. I...
I know that there are several different answers that has already been posted for this. I have tried all of the options but wanted to see if I can get a correct one that actually works. Assume that a gallon of paint covers about 350 square feet of wall space. Create an application with a main() method that prompts the user for the length, width, and height of a rectangular room. Pass these three values to a method that does...
I am trying to do edge detection using matlab. I have posted code here. It does...
I am trying to do edge detection using matlab. I have posted code here. It does not give any error but it's not running it at all. So please help me to fix it and also exaplin each line of this code pls. Thanks! i = imread('C:\Users\Amanda\Desktop"); I = rgb2gray(1i); BW1 = edge(I,'prewitt'); BW2= edge(I,'sobel'); BW3= edge(I,'roberts'); subplot (2,2,1); imshow(I); title('original'); subplot(2,2,2); imshow(BW1); title('Prewitt'); subplot(2,2,3); imshow(BW2); title('Sobel'); subplot(2,2,4); imshow(BW3); title('Roberts');
I NEED AN ESSAY OF 1000 WORDS AT LEAST BECAUSE I ALREADY POSTED THIS QUESTOIN I...
I NEED AN ESSAY OF 1000 WORDS AT LEAST BECAUSE I ALREADY POSTED THIS QUESTOIN I GOT SHORT ANSWER .PLEASE HELP Using the human resource department for recruitment, training, development and retaining employees can give any organization a competitive advantage. Some organizations became very successful in their markets by establishing an employee centered culture. Discuss how HRM activities enable organizations to become more competitive in their markets. Use an example of a well known organization that had been successful. In...
If I am looking to maximize my investment as an investor and the company I am...
If I am looking to maximize my investment as an investor and the company I am looking at has a low or unstable payout schedule, what would be the point of investing?
I am writing a paper on the dealiest epidemics in recorded history. I am looking for...
I am writing a paper on the dealiest epidemics in recorded history. I am looking for information on the polio virus, thypoid fever, and the black palgue. I need help identifying when these diseases started, what started them, the virus or mutation that caused them, how they spread and the preventative measures to stop them.
I am writing a paper on the dealiest epidemics in recorded history. I am looking for...
I am writing a paper on the dealiest epidemics in recorded history. I am looking for information on typhus epidemic/camp fever. I need help identifying when these diseases started, what started them, the virus or mutation that caused them, how they spread and the preventative measures to stop them.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT