March 14th, 2003, 12:30 PM
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.
March 14th, 2003, 01:42 PM
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 01:44 PM.
March 14th, 2003, 02:55 PM
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:
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++.
March 14th, 2003, 03:00 PM
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:
Of course, if BYTE is defined on your system, substitute BYTE for "unsigned char".
// strictly from memory and off the top of my head
// if it doesn't work, then play with it until it does.
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--)
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 .
March 14th, 2003, 03:05 PM
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.
March 14th, 2003, 03:27 PM
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. :)
March 14th, 2003, 03:30 PM
Yes, I recall the same thing. Type casting in TP is actually bit-casting (yes, I just made that word up).