473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to convert 16-bit binary fraction to a float

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
Nov 13 '05 #1
4 10695
Mac
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?

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
--
Nov 13 '05 #2
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
--

Nov 13 '05 #3

"music4" <mu****@163.net> wrote in message
news:bk********@netnews.proxy.lucent.com...
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
Nov 13 '05 #4
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.

Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michel | last post by:
Hi again all, I have a small issue. Here's an example dataset : F1 F2 F3 1 0.58 Hi 2 0.70 Hello 3 Fail Bye 4 <Null> Hi
4
by: Ross | last post by:
Anyone have some code to help me understand how I can convert a given hex string, say "0009dbaa00004c00000000fb82ca621c," into a struct of this form: struct uniqueid { ulong32_t word1;...
3
by: aaa | last post by:
I have a third party DLL that downloads everything from a device to a particular device into the following Hex format: 080 : 13 70 05 00 (Timestamp) 081 : 35 E1 91 F9 9F BF (Mode 1 Supported...
6
by: Tim Cartwright | last post by:
I have a page that has the login control on it, nothing else. This page inherits from a master page, neither page has any code in it. This page works perfectly when running on the WebDev debug web...
6
by: Glenn Wilson | last post by:
I have converted most of the code that I have but am having trouble, mainly with the marked lines. (>>) public static UInt16 checksum( UInt16 buffer, int size ) { Int32 cksum = 0; int counter;...
5
by: rsanan | last post by:
How do I convert a datetime from en-GB to en-US format here is my code - (not working for the clients outside of US) /*******************CODE*****************/ System.Globalization.CultureInfo...
8
by: deepak_kamath_n | last post by:
Hello, I have the following scenario: 1. My application receives the date from another application as a string 2. The other application is running in a different time zone as compared to my...
4
by: krishp | last post by:
Hi, I need to convert raw data recieved from a device through C#.Net socket to BMP format. For some reason the image conversion is not happening proparly and I can see only blur image . ...
14
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
4
by: ahmurad | last post by:
Dear Brothers, I am struggling the following four Date-Time type values which were inputted into MYSQL database in different tables. As MYSQL Default Time Format: YYYY-MM-DD HH:MM:SS, So I used...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.