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

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

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

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

  • 2. This function compares two numbers and returns them in increasing order. 1. Fill in the...

    2. This function compares two numbers and returns them in increasing order. 1. Fill in the blanks, so the print statement displays the result of the function call in order. 1 # This function compares two numbers and returns them 2 # in increasing order. 3 - def order_numbers(number1, number2): 4. if number2 > number1: return numberi, number2 6 else: return number2, number1 9 Run # 1) Fill in the blanks so the print statement displays the result # of...

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

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

  • Using C++ please create a program for a first grader to practice subtractions. The program randomly...

    Using C++ please create a program for a first grader to practice subtractions. The program randomly generates two single-digit integers number1 and number2. Ask the user to input the difference of the two numbers with always the greater number as the first operand. If the user inputs wrong answer, print a message, “Your answer is wrong. The correct answer is ” with the correct answer. Otherwise print “Your answer is correct.” Hint: 1. Check if number1 < number2, otherwise swap...

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

  • I must call multiply, divide and remainder functions inside of SUBTRACT function.. I am stuck at...

    I must call multiply, divide and remainder functions inside of SUBTRACT function.. I am stuck at this point. Thank you #include<stdio.h> int getDifference(int a, int b); int main() { int a, b, result, multiply, divide, rem; printf("Enter the two integer numbers : "); scanf("%d %d", &a, &b); //Call Function With Two Parameters result = getDifference(a, b);    printf("Difference of two numbers is : %d\n" , result);    return (0); } int getDifference(int a, int b) { int c, multiply; float...

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
ADVERTISEMENT