Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7410237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:12:18+00:00 2026-05-29T06:12:18+00:00

I wrote this code to do the IEEE 754 floating point arithmetic on a

  • 0

I wrote this code to do the IEEE 754 floating point arithmetic on a 4byte string.

It takes in the bytes, converts them to binary and with the binary I get the sign, exponent, and mantissa and then do the calculation.

It all works just about perfectl, 0xDEADBEEF gives me 6259853398707798016 and the true answer is 6.259853398707798016E18, now these are same values and I wont have anything this large in the project I’m working with, all other smaller values put the decimal in the correct place.

Here is my code:
float calcByteValue(uint8_t data[]) {
int i;
int j = 0;
int index;
int sign, exp;
float mant;

   char bits[8] = {0};
   int *binary = malloc(32*sizeof *binary);
   for (index = 0;index < 4;index++) {
      for (i = 0;i < 8;i++,j++) {
         bits[i] = (data[index] >> 7-i) & 0x01;
         if (bits[i] == 1) {
            binary[j] = 1;
         } else {
            binary[j] = 0;
         }
      }
      printf("\nindex(%d)\n", index);
   }

   sign = getSign(&(binary[0]));
   mant = getMant(&(binary[0]));
   exp = getExp(&(binary[0]));

   printf("\nBinary: ");
   for (i = 0;i < 32;i++)
      printf("%d", binary[i]);
   printf("\nsign:%d, exp:%d, mant:%f\n",sign, exp, mant);

   float f = pow(-1.0, sign) * mant * pow(2,exp);
   printf("\n%f\n", f);
   return f;
}

//-------------------------------------------------------------------

int getSign(int *bin) {
   return bin[0];
}

int getExp (int *bin) {
     int expInt, i, b, sum;
     int exp = 0;

     for (i = 0;i < 8;i++) {
        b = 1;
        b = b<<(7-i);
        if (bin[i+1] == 1)
           exp += bin[i+1] * b;
     }

     return exp-127;

}

float getMant(int *bin) {
   int i,j;
   float b;
   float m;
   int manBin[24] = {0};
   manBin[0] = 1;
   for (i = 1,j=9;j < 32;i++,j++) {
       manBin[i] = bin[j];
       printf("%d",manBin[i]);
   }
   for (i = 0;i < 24;i++) {
      m += manBin[i] * pow(2,-i);;
   }
   return m;
}

Now, my teacher told me that there is a much easier way where I can just take in the stream of bytes, and turn it into a float and it should work. I tried doing it that way but could not figure it out if my life depended on it.

I’m not asking you to do my homework for me, I have it done and working, but I just need to know if I could of done it differently/easier/more efficiently.

EDIT: there are a couple special cases I need to handle, but it’s just things like if the exponent is all zeros blah blah blah. Easy to implement.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:12:19+00:00Added an answer on May 29, 2026 at 6:12 am

    Here is my working code:

        unsigned char val[4] = {0, 0, 0xc8, 0x41};
        cout << val << endl;
    
        cout << "--------------------------------------------" << endl;
        float f = *(float*)&val;
    
        cout << f << endl;
        return 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to get the raw bytes of a floating point (IEEE-754) Number
I wrote this code in C# to encrypt a string with a key: private
I wrote this code in C: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h>
I wrote this code today to check frequency of each character in string. It
I wrote this code: string Dcname = DataClasses1DataContext; string TableName = Order; var dc
I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
I wrote this code. The constructor works normally, but in the destructor I get
If I wrote this code: typeof(myType).TypeHandle Would it use reflection? How much different from:
Hi guys I wrote this code and i have two errors. Invalid rank specifier:
I was trying to understand something with pointers, so I wrote this code: #include

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.