how to convert 16-bit binary fraction to a float

Discussion in 'C Programming' started by music4, Sep 19, 2003.

  1. music4

    music4 Guest

    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
    music4, Sep 19, 2003
    #1
    1. Advertising

  2. music4

    music4 Guest

    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
    > --
    music4, Sep 19, 2003
    #2
    1. Advertising

  3. music4

    Mac Guest

    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
    --
    Mac, Sep 19, 2003
    #3
  4. music4

    Mike Wahler Guest

    "music4" <> wrote in message
    news:bkdu2o$...
    > 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
    Mike Wahler, Sep 19, 2003
    #4
  5. music4

    Lew Pitcher Guest

    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.
    Lew Pitcher, Sep 20, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fangs
    Replies:
    3
    Views:
    9,766
    darshana
    Oct 26, 2008
  2. bd
    Replies:
    0
    Views:
    606
  3. Replies:
    12
    Views:
    566
    Richard Heathfield
    Apr 8, 2007
  4. nas
    Replies:
    10
    Views:
    500
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Jun 19, 2007
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,509
    James Kanze
    Oct 8, 2009
Loading...

Share This Page