Velocity Reviews > how to convert 16-bit binary fraction to a float

# how to convert 16-bit binary fraction to a float

music4
Guest
Posts: n/a

 09-19-2003
Greetings,

I psinfo_t struct, pr_pctcpu is ushort_t type with comment: "pr_pctcpu is 16
bit binary fractions in the range 0.0 to 1.0 with the binary point to the
right of the high-orfer bit (1.0=0x8000)." My question is how to convert it
to a float?

Evan

music4
Guest
Posts: n/a

 09-19-2003
Mac,

Thanks for your answer. The description is from Sun's document. Now I got
the solution. It's not easy to describ the arithmetic in one secence.

By the way, I am not a student, so surely that's not homework.

Evan

> It's hard to tell for sure what you mean, but it sounds as though all you
> have to do is this:
>
>
> double d;
>
> ...
>
> d = pr_pctcpu / (double)0x8000;
>
>
> I hope this is not a homework assignment?
>
> Mac
> --

Mac
Guest
Posts: n/a

 09-19-2003
On Fri, 19 Sep 2003 11:42:53 +0800, music4 wrote:

> Greetings,
>
> I psinfo_t struct, pr_pctcpu is ushort_t type with comment: "pr_pctcpu is 16
> bit binary fractions in the range 0.0 to 1.0 with the binary point to the
> right of the high-orfer bit (1.0=0x8000)." My question is how to convert it
> to a float?
>
> Evan

It's hard to tell for sure what you mean, but it sounds as though all you
have to do is this:

double d;

....

d = pr_pctcpu / (double)0x8000;

I hope this is not a homework assignment?

Mac
--

Mike Wahler
Guest
Posts: n/a

 09-19-2003

"music4" <(E-Mail Removed)> wrote in message
news:bkdu2o\$(E-Mail Removed)...
> Greetings,
>
> I psinfo_t struct, pr_pctcpu is ushort_t type with comment: "pr_pctcpu is

16
> bit binary fractions in the range 0.0 to 1.0 with the binary point to the
> right of the high-orfer bit (1.0=0x8000)." My question is how to convert

it
> to a float?

Assuming that type 'ushort_t' is a typedef for 'unsigned short'
and that it occupies exactly sixteen bits (might be larger
on some implementations):

#include <stdio.h>

float cvt(unsigned short value)
{
return (value >> 0xF) + (value & 0x7FFF)
/ (float)(1 << 0xF);
}

int main()
{
unsigned short data = 0;

for(data = 0; data <= 0x8000; ++data)
printf("data == 0x%X : as float == %f\n", data, cvt(data));

return 0;
}

-Mike

Lew Pitcher
Guest
Posts: n/a

 09-20-2003
music4 wrote:
> Greetings,
>
> I psinfo_t struct, pr_pctcpu is ushort_t type with comment: "pr_pctcpu is 16
> bit binary fractions in the range 0.0 to 1.0 with the binary point to the
> right of the high-orfer bit (1.0=0x8000)." My question is how to convert it
> to a float?

From your brief and slightly obscure description, I'd say that the pr_pctcpu
value can be read (bitwise) like this...
B.bbbbbbbbbbbbbbb
such that
0x8000 = 1.0
0x4000 = 0.5
0x2000 = 0.25
0x1000 = 0.125
etc.
Each bit position represents 1/2 the value of the bit position to it's left,
with the leftmost bit representing 1

Knowing this, you could just loop, extracting bits, adding and dividing

That is to say...

float ConvertFixedPtBinary(unsigned short FixedPtBin)
{
float temp = 0.0;
int bit;

for (bit = 0; bit < 16; ++bit)
{
temp = temp / 2.;

if (FixedPtBin & 1) temp = temp + 1.;

FixedPtBin >>= 1;
}
return temp;
}

--
Lew Pitcher

Master Codewright and JOAT-in-training
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.