Hagi
Hagi

Reputation: 87

Return the mantissa as bits

I got a bit trouble with my c code. I am trying to extract the sign, exponent and the mantissa. So far it worked out for sign and exponent. The problem begins with mantissa. For example, let's take -5.0. 1 bit for sign, 8 bits for the exponent and 23 bits for the mantissa. This is the binary notation of -5.0:

1 10000001 01000000000000000000000

1 = sign

10000001 = exponent

01000000000000000000000 = mantissa.

I wanna return the bits of the mantissa back.

static int fpmanitssa(uint32_t number) {

   uint32_t numbTemp = 8388607;
   number = number & numbTemp;  //bitwise AND, number should be 2097152.

   int numbDiv;
   int leftover;
   char resultString[23];
   size_t idx = 0;

   do {
       numbDiv = number/2;
       leftover = number % 2;
       resultString[idx++] = leftover;
       number = numbDiv;
   } while (number != 0);

 return resultString;

What I get as result are negative numbers. I don't know why it isn't working. Could you please help to find the problem?

regards Hagi

Upvotes: 1

Views: 829

Answers (3)

Chad Barth
Chad Barth

Reputation: 568

For one thing, your function declaration specifies that you are returning an int, but then you return resultString, which is a char*. I think you may be relying on automatic type conversion which is not part of C. To get resultString to even look like what you want, you need to add 0x30 to leftover (0x30 is '0' and 0x31 is '1').

Then you would need to null terminate the string and convert it back to an int with atoi().

The following probably doesn't work exactly as you intend, but includes changes to handle the ascii/integer conversions:

static int fpmanitssa(uint32_t number) {

   uint32_t numbTemp = 8388607;
   number = number & numbTemp;  //bitwise AND, number should be 2097152.

   int numbDiv;
   int leftover;
   char resultString[24] = {0};
   size_t idx = 0;

   do {
       numbDiv = number/2;
       leftover = number % 2;
       resultString[idx++] = leftover + 0x30;
       number = numbDiv;
   } while (number != 0);

 return atoi(resultString);
}

Upvotes: 0

hroptatyr
hroptatyr

Reputation: 4829

If you happen to be on uClibc or glibc, you can use ieee754.h and the mantissa field, like so

#include <ieee754.h>

static unsigned int fpmanitssa(float number)
{
        ieee754_float x = {.f = number};
        return x.mantissa;
}

Upvotes: 0

Eric Postpischil
Eric Postpischil

Reputation: 223464

1) Floating-point numbers have significands (with linear scale, also called the fraction portion), not mantissas (with logarithmic scales).

2) Your function is declared to return an int, but you attempt to return resultString, which is an array of char. An array of char is not an int and cannot be automatically converted to an int.

3) If you did return resultString, it would fail because the lifetime of resultString is only the time during function execution; it cannot be used after the function returns. If you do intend to return characters from the function, it should be declared so that the caller passes in an existing array of suitable size or the function dynamically allocates an array (as with malloc) and returns its address to the caller, who is responsible for eventually deallocating it (with free).

4) If you want to return an int with the bits of the encoded significand, and number contains the encoded floating-point number with the encoded significand in the low bits, then all you have to do is remove the high bits with an AND operation and return the low bits.

5) If the array of char is to form a printable numeral rather than numeric bits, then '0' should be added to each one, to convert it from the integer 0 or 1 to the character “0” or “1”. In this case, you may also want a null character at the end of resultString.

6) The bits are assigned to resultString in order from low bit to high bit, which may be the opposite order from desired if resultString is intended to be printed later. (The order may be fine if resultString will only be used for calculation, not printing.)

7) The above presume you want to return only the encoded significand, which is the explicit bits in the floating-point encoding. If you want to return the represented significand, including the implicit bit, then you need a 24th position, and it is 1 if the exponent is non-zero and 0 otherwise. (And presuming, of course, that the number is not a NaN or infinity.)

Upvotes: 1

Related Questions