Thread: Manipulate the bits and/or byte of a floating point variable

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    1

    Manipulate the bits and/or byte of a floating point variable

    I am trying to convert a non IEEE 754 FP value to 754 where the bit pattern representation is slightly different.

    How can I get C program access to the various bits and/or bytes of the FP value?

    Thanks,

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Probably something like
    Code:
    union {
      float f_val;
      unsigned long int u_val;
    } both;
    Mess about with the bits in both.u_val and read out the float in both.f_val
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Or trick the compiler...
    Code:
    float var = ...;
    int var_bits = *(int*)&var;
    Then you can use bitwise operators with the var_bits variable, if float size is the same as int... which is by standard.
    The union on Salem's post is also a perfectly valid option...

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Quote Originally Posted by xErath
    Or trick the compiler...
    [code]
    if float size is the same as int... which is by standard.
    Are you sure?
    hello, internet!

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    If you're messing with the bits, you can use bitfields:
    Code:
    union MyUnion
    {
       float theFloat;
       struct Bits
       {
          int a : 1;
          int b : 1;
          //... one variable for each bit
       }theBits;
    };
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by moi
    Are you sure?
    ...
    Its rare to find anyone with a non-32 bit computer, and non-x86 compatible compiler...

  7. #7
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Quote Originally Posted by xErath
    ...
    Its rare to find anyone with a non-32 bit computer, and non-x86 compatible compiler...
    A very dissapointing line of reasoning.
    hello, internet!

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I think his query refers to your assertion that it is 32 bits 'by standard' It is only guaranteed to be >= short and <= long. What if mkh goes on to develop programs for other platforms in the future?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Bah you guys...!!!
    Nowadays most of computers ARE 32bit!!!!!
    Most 32 bit compilers use 32 bit long ints, and 32 bit long floats...
    Who said it'll be always like this?
    Was I clear??....

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Nowadays most of computers ARE 32bit!!!!!
    So what if you're mostly right (which is doubtful), it won't be too long before your assumtion will be false. 64bit is emerging at the high end, and sooner or later it will work its way down to your desktop.

    There are vastly more embedded processors in the world, many of which do not fit your standard assumption of a 32 bit world.

    Best long term plan is to not get too "chummy" with this idea that everything is 32 bit.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Salem
    > Nowadays most of computers ARE 32bit!!!!!
    So what if you're mostly right (which is doubtful), it won't be too long before your assumtion will be false. 64bit is emerging at the high end, and sooner or later it will work its way down to your desktop.
    Most doesn't mean 100%, 2# 64 bit computers are 'emerging', but have not yet emerged.

    And standard was a bad word... I mean default.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by xErath
    Most doesn't mean 100%, 2# 64 bit computers are 'emerging', but have not yet emerged.

    And standard was a bad word... I mean default.
    What do you mean by "not yet emerged"? When I can buy an AMD64 based computer at my local Best Buy, I'd say they're more than "emerging".

    Anyway, yes, when you say the word "standard" here, we pedantics think you mean The Standard(TM).

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The Ten Commandments for C Programmers #10 "All the world's a VAX":
    This particular heresy bids fair to be replaced by "All the world's a Sun" or "All the world's a 386" (this latter being a particularly revolting invention of Satan), but the words apply to all such without limitation. Beware, in particular, of the subtle and terrible "All the world's a 32-bit machine", which is almost true today but shall cease to be so before thy resume grows too much longer.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    Can anyone enlighten me? Why tihis... this crap prints 12? It seems to have something to do with long long, because Single Precision analog did work perfectly. I use VC.NET 2003
    Code:
    #include <stdio.h>
    
    #pragma pack(push, 1)
    typedef union
    {
        double dValue;
        struct
        {
            unsigned long long fraction:52;
            unsigned int       exponent:11;
            unsigned int       sign:1;
        } sValue;
    } DP_FPN;
    #pragma pack(pop)
    
    #define PRINT(x, type) printf(#x " = %" #type "\n", x);
    
    int main(void)
    {
        PRINT(sizeof(DP_FPN), d);
    
        return 0;
    }

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read my post in this thread.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For the numerical recipes in C types!
    By Smattacus in forum C Programming
    Replies: 5
    Last Post: 10-28-2008, 07:57 PM
  2. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  3. casting non-lvalues
    By ... in forum C Programming
    Replies: 20
    Last Post: 08-23-2005, 11:56 PM
  4. Floating point exceptions
    By Boris in forum C++ Programming
    Replies: 8
    Last Post: 11-19-2001, 08:32 AM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM