Question

using C , comments will be appreciated. Question1 Write down an function named bitwisedFloatCompare(float number1, float...

using C , comments will be appreciated.
Question1
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.
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).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;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*

* 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

//

//***********************************************************************************/

#include <stdio.h>

int bitwisedFloatCompare(float number1,float number2)

{

int answer = 0;

int b1;

int *bits = &b1;

unsigned int *x, *y;

x = (unsigned int *)(void *)&number1;

y = (unsigned int *)(void *)&number2;

for (*bits = 31; *bits>= 0; (*bits)--) {

if ((*x & (1u << *bits))

&& !(*y & (1u << *bits))) {

answer = 1;

break;

}

else if (!(*x & (1u << *bits)) && (*y & (1u << *bits))) {

answer = -1;

break;

}

}

if (*bits == 31)

answer = -answer;


return answer;


}


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;

}

************************************************************************************************************************

SEE OUTPUT

Files main. c B saved main.c clang version 7.0.0-3-ubuntu0.18.04.1 (ta S/RELEASE_700/final) > clang-7 -pthread -lm -o main ma


Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
using C , comments will be appreciated. Question1 Write down an function named bitwisedFloatCompare(float number1, float...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 1: Write down an function named bitwisedFloatCompare(float number1, float number2) that tests whether a floating...

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

  • i have no idea how to do floating point comparison this is c programming CHALLENGE 3.16.1:...

    i have no idea how to do floating point comparison this is c programming CHALLENGE 3.16.1: Floating-point comparison: Print Equal or Not equal Write an expression that will cause the following code to print "Equal" if the value of sensorReading is "close enough" to targetValue. Otherwise, print "Not equal". Ex: If targetValue is 0.3333 and sensorReading is (1.0/3.0), output is Equal 1 #include <stdio.h 2 #include <math.h> 4 int main(void)f 6 8 scanf("%lf", &targetValue); 1 test passed double targetValue; double...

  • In the following code, it gets hung up at    cout << "Number1 * Number2 =...

    In the following code, it gets hung up at    cout << "Number1 * Number2 = " << number1 * number2 << endl; giving an error of "no math for operator<<" what am i doing wrong? Thank you #include <iostream> #include <cctype> #include <cstdlib> using namespace std; class Rational //class for rational numbers (1/2, 5/9, ect..) {    public:        Rational(int numerator, int denominator);        Rational(int numberator);        Rational(); //default        friend istream& operator >>(istream& ins,...

  • Question 17 (3 points) Predict the output of the following code segment: a = 99.98 if...

    Question 17 (3 points) Predict the output of the following code segment: a = 99.98 if a + 0.01 >= 100: print('A') elif a + 0.02 >= 100: print('B') print('c') else: print('D') print('E') Question 16 (3 points) Predict the output of the following code segment: X = 6 if x % 2 == 0: print ("x is even.") else: print ("x is odd.") Please use the decimal point to distinguish int and float. For example, number 3 + number 4...

  • 61. The following program is accepted by the compiler:         int sum( int x, int y...

    61. The following program is accepted by the compiler:         int sum( int x, int y )         {             int result;             result = x + y;            }                            T__   F__ 62. The following implementation is accepted by the compiler:         void product()         {             int a; int b; int c; int result;             cout << "Enter three integers: ";             cin >> a >> b >> c;             result = a * b * c;            ...

  • there is a function to create two random numbers between 1 and 25 and a function...

    there is a function to create two random numbers between 1 and 25 and a function to add them together. add the prototypes correctly, call them from main correctly and output the results. The three functions should be subtractNumbers, multiplyNumbers, and divideNumbers. Remember that division won't always yield an integer so be sure to take care of that issue within the function (don't change the variable declarations). Don't change the code that I've given you. Follow the same pattern. #include...

  • 2. Write a program with three functions that will be called from the main function The functions will calculate the...

    2. Write a program with three functions that will be called from the main function The functions will calculate the volume, area and perimeter of a cuboid. You should able to pass three parameters such as length, width and height, and a function will return area, another will return volume and final one will return perimeter. Define all your variables in floating point numbers 3. Modify the problem#2 to calculate the volume, area and perimeter in a sing le function...

  • C programming Question1 (a) Write a C program that will print out all command line arguments,...

    C programming Question1 (a) Write a C program that will print out all command line arguments, in reverse order, one per line. Prefix each line with its index. 6 marks] (b) Consider this C code snippet int a- 100 int b- 42; inte p- &a; int q-b; p qi printf ("%d %d\n" ,a,*p); When this code is executed, what numbers will it print? [2 marks] (c) Consider this C program int main(int argc,char argv) char* target- "Apple" char vord[100] printf...

  • Write a complete C program. Define a structure type element_t to represent one element from the...

    Write a complete C program. Define a structure type element_t to represent one element from the periodictable of elements. Components should include the atomic number (aninteger); the name, chemical symbol, and class (strings); a numeric field forthe atomic weight; and a seven-element array of integers for the number ofelectrons in each shell. The following are the components of an element_t structure for sodium.11 Sodium Na alkali_metal 22.9898 2 8 1 0 0 0 0 Have the user enter the data...

  • C linux please write it by only using “if and else” conditions, thanks so much this...

    C linux please write it by only using “if and else” conditions, thanks so much this is my task 3 Requirements 1. Copy your solution for Task 3 t3.c to a new file t4.c. 2. The input is guaranteed to contain at least one non-whitespace character. 3. If the input is well-formed, i.e., can be parsed to a number, the program should behave identically to Task 3. All the requirements of Task 3 still apply except the file name. 4....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT