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.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Edward A. Falk C Programming 1 04-04-2013 08:07 PM Carsten Fuchs C++ 45 10-08-2009 09:47 AM nas C++ 10 06-19-2007 09:03 PM Andy Java 7 05-10-2004 09:26 PM bd C Programming 0 07-07-2003 12:09 AM