float and 1's

Discussion in 'C Programming' started by Vijay Kumar R Zanvar, Jan 2, 2004.

  1. Hi,
    Just like counting the number of bits set to
    1 in an integral variable, can we count the same
    in a float?

    I have two solutions in my mind:

    1.
    union {
    float f;
    int i;
    }u = { 10 };

    Now, count the required number from u.i.
    Will this work?


    2.
    float f;
    unsigned char uc[sizeof (float)];

    memcpy ( uc, &f, sizeof (float) );

    Now count from the array. How about this?


    Season's Greetings!

    --
    Vijay Kumar R Zanvar
    My Home Page - http://www.geocities.com/vijoeyz/
    Vijay Kumar R Zanvar, Jan 2, 2004
    #1
    1. Advertising

  2. Vijay Kumar R Zanvar

    lallous Guest

    "Vijay Kumar R Zanvar" <> wrote in message
    news:bt3kt1$30ugj$-berlin.de...
    > Hi,
    > Just like counting the number of bits set to
    > 1 in an integral variable, can we count the same
    > in a float?
    >
    > I have two solutions in my mind:
    >
    > 1.
    > union {
    > float f;
    > int i;
    > }u = { 10 };
    >
    > Now, count the required number from u.i.
    > Will this work?
    >
    >
    > 2.
    > float f;
    > unsigned char uc[sizeof (float)];
    >
    > memcpy ( uc, &f, sizeof (float) );
    >
    > Now count from the array. How about this?
    >
    >
    > Season's Greetings!
    >
    > --
    > Vijay Kumar R Zanvar
    > My Home Page - http://www.geocities.com/vijoeyz/
    >
    >

    Hello,

    It is enough to declare the float then take a char* to it as:
    float f;
    char *p = &f;
    // now do the bit counting in 'p' till sizeof(f) , making at most
    sizeof(f)*8 bits

    --
    Elias
    lallous, Jan 2, 2004
    #2
    1. Advertising

  3. lallous wrote:

    > Hello,
    >
    > It is enough to declare the float then take a char* to it as:
    > float f;
    > char *p = &f;


    You need an explicit cast here. And it should be unsigned char*
    probably.

    unsigned char *p = (unsigned char *)&f;

    > // now do the bit counting in 'p' till sizeof(f) , making at most
    > sizeof(f)*8 bits


    Note that this will also count any padding bits if there are any.

    --
    Thomas.
    Thomas Stegen, Jan 2, 2004
    #3
  4. "Vijay Kumar R Zanvar" <> writes:
    > Just like counting the number of bits set to
    > 1 in an integral variable, can we count the same
    > in a float?
    >
    > I have two solutions in my mind:
    >
    > 1.
    > union {
    > float f;
    > int i;
    > }u = { 10 };
    >
    > Now, count the required number from u.i.
    > Will this work?


    Not reliably. Note that float and int may or may not be the same
    size. (As a matter of style, I'd use "10.0", or even "10.0f", rather
    than "10" in the initialization to make it clear that you're
    initializing the float member.)

    > 2.
    > float f;
    > unsigned char uc[sizeof (float)];
    >
    > memcpy ( uc, &f, sizeof (float) );
    >
    > Now count from the array. How about this?


    Yes, that should work, though others have pointed out that you don't
    really need to copy f to an array.

    BTW, I can't think of any use for the number of 1 bits in a float
    other than idle curiosity -- not that there's anything wrong with idle
    curiosity.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
    (Note new e-mail address)
    Keith Thompson, Jan 2, 2004
    #4
  5. Vijay Kumar R Zanvar

    Jack Klein Guest

    On Fri, 2 Jan 2004 13:49:15 +0200, "lallous" <> wrote
    in comp.lang.c:

    > "Vijay Kumar R Zanvar" <> wrote in message
    > news:bt3kt1$30ugj$-berlin.de...
    > > Hi,
    > > Just like counting the number of bits set to
    > > 1 in an integral variable, can we count the same
    > > in a float?
    > >
    > > I have two solutions in my mind:
    > >
    > > 1.
    > > union {
    > > float f;
    > > int i;
    > > }u = { 10 };
    > >
    > > Now, count the required number from u.i.
    > > Will this work?
    > >
    > >
    > > 2.
    > > float f;
    > > unsigned char uc[sizeof (float)];
    > >
    > > memcpy ( uc, &f, sizeof (float) );
    > >
    > > Now count from the array. How about this?
    > >
    > >
    > > Season's Greetings!
    > >
    > > --
    > > Vijay Kumar R Zanvar
    > > My Home Page - http://www.geocities.com/vijoeyz/
    > >
    > >

    > Hello,
    >
    > It is enough to declare the float then take a char* to it as:
    > float f;
    > char *p = &f;
    > // now do the bit counting in 'p' till sizeof(f) , making at most
    > sizeof(f)*8 bits


    Aside from what others have said, your very last sentence is
    completely wrong. I am working on a platform right now where CHAR_BIT
    is 16. sizeof(float) is 2, and a float contains 32 bits.

    It is definitely possible for the binary representation of a float to
    have more than sizeof(float)*8 1 bits. In fact it could have as many
    as sizeof(float)*16 1 bits.

    But it will never, ever have more than sizeof(float)*CHAR_BIT 1 bits.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Jan 2, 2004
    #5
  6. [..]

    >
    > BTW, I can't think of any use for the number of 1 bits in a float
    > other than idle curiosity -- not that there's anything wrong with idle
    > curiosity.
    >


    You are right. It was only my curiosity.

    Thanks
    vijay-z.
    Vijay Kumar R Zanvar, Jan 4, 2004
    #6
    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. bd
    Replies:
    0
    Views:
    608
  2. Alexander Malkis
    Replies:
    10
    Views:
    889
  3. float ** and float *p[]

    , Dec 6, 2006, in forum: C Programming
    Replies:
    8
    Views:
    349
    Barry Schwarz
    Dec 7, 2006
  4. Florian Lindner
    Replies:
    1
    Views:
    626
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,509
    James Kanze
    Oct 8, 2009
Loading...

Share This Page