#1
  1. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    13

    how to display individual bits of float (?)


    I want to display the individual bits of a float, double, or long double. How can I "cast" this (without modification of the bits) to an unsigned integer type, so I can easily display the exact bits of the original float, double, or long double? The key note is casting without modification of the bits, remember. I doubt it is even called casting, but for lack of a better word I used it.
    Matthew Doucette / Xona.com
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    I don't have a dev environment handy at the moment, but I think the only way to do it without changing the data would be to typecast pointers to the data.

    Obtain a pointer to your float (or whatever).

    Cast it to say, a short*.

    Iterate through as many short* as needed to accomidate the sizeof float/double/whatever.

    Each iteration should iterate through each bit of the respective dereferenced short*.
    Last edited by MJEggertson; March 14th, 2003 at 12:44 PM.
  4. #3
  5. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    MJEggertson, that is what I was thinking. But, since the type of a pointer in C is not just 'pointer' but rather 'pointer to type', you would have to type cast the pointer itself to be able to assign the pointer to short a pointer to float. But, I assume that this would work.

    I do know in Turbo Pascal you can do this:

    x: longint;
    xhigh,xlow: word absolute x;
    (note that longint = 32-bit integer, and word = 16-bit integer)

    Which makes the variables xhigh and xlow access the same memory as the high and low words of x. The compiler produces code that makes these variables believe that they are their own variables, but in fact are accessing the same memory. I do not know if this is possible in C/C++.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,158
    Rep Power
    2222
    I've done it before and I'm relying on my memory since I forget where that code segment is (a bad sign?).

    Every variable in memory is in a contiguous string of bytes, so "simply" cast a pointer to your floating-point variable to a byte pointer and access each byte as if in an array of bytes, displaying each byte in hex in order to read the bits more easily:

    Code:
    // strictly from memory and off the top of my head
    // if it doesn't work, then play with it until it does.
    
    double  d;
    unsigned char *bp;  // the byte pointer
    
    bp = (unsigned char*)&d;
    // I'm sure there are more elegant ways, but it's lunchtime 
    //     so I'm going for quick-&-dirty
    // Please note that this is for Intel's little-endian storage of multi-byte values
    for (int i=7; i>=0; i--)
            printf("%02X ",bp[i]);
    printf("\n");
    Of course, if BYTE is defined on your system, substitute BYTE for "unsigned char".

    Also be sure to note that this approach depends on knowing whether your system is big-endian or little-endian. All the Intel systems (DOS & Windows) I've worked on have been little-endian (to my knowledge), so my example reflects that. You will need to adjust for your system.

    Floating-point binary format is defined in IEEE Standard 754; eg at http://research.microsoft.com/~holla...ieeefloat.html .
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,158
    Rep Power
    2222
    Originally posted by Jason Doucette

    I do know in Turbo Pascal you can do this:

    x: longint;
    xhigh,xlow: word absolute x;
    (note that longint = 32-bit integer, and word = 16-bit integer)

    Which makes the variables xhigh and xlow access the same memory as the high and low words of x. The compiler produces code that makes these variables believe that they are their own variables, but in fact are accessing the same memory. I do not know if this is possible in C/C++.
    As I recall as I was making the transition from TP to C many years ago, the two do type-casting entirely differently. TP does access the same memory but interprets the bytes there as if they were of the new type. C actually converts the original value to the new type, which can have an entirely different bit-pattern.

    In order to do a TP-type type-cast in C, then you do need to set a pointer to the original variable and typecast the pointer to a pointer of the new type.
  10. #6
  11. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Originally posted by Jason Doucette
    I do know in Turbo Pascal you can do this:

    x: longint;
    xhigh,xlow: word absolute x;
    (note that longint = 32-bit integer, and word = 16-bit integer)

    Which makes the variables xhigh and xlow access the same memory as the high and low words of x. The compiler produces code that makes these variables believe that they are their own variables, but in fact are accessing the same memory. I do not know if this is possible in C/C++.
    You can do this with unions. A union is a user-defined data type that can hold values of different types at different times. It is similar to a structure except that all of its members start at the same location in memory. I knew C/C++ could do this; I just couldn't remember. :)
  12. #7
  13. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Originally posted by dwise1_aol
    As I recall as I was making the transition from TP to C many years ago, the two do type-casting entirely differently. TP does access the same memory but interprets the bytes there as if they were of the new type. C actually converts the original value to the new type, which can have an entirely different bit-pattern.

    In order to do a TP-type type-cast in C, then you do need to set a pointer to the original variable and typecast the pointer to a pointer of the new type.
    Yes, I recall the same thing. Type casting in TP is actually bit-casting (yes, I just made that word up).

IMN logo majestic logo threadwatch logo seochat tools logo