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.