how to convert 16-bit binary fraction to a float

M

music4

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?

Thanks in advance!
Evan
 
M

music4

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
 
M

Mac

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?

Thanks in advance!
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
--
 
M

Mike Wahler

music4 said:
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
 
L

Lew Pitcher

music4 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top