|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
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.
|
|
#2
|
|||
|
|||
|
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. |
|
#3
|
||||
|
||||
|
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++.
__________________
Jason Doucette / Xona.com™ - Programming Windows Errata Addendum "Discussion is an exchange of knowledge; argument is an exchange of ignorance." |
|
#4
|
||||
|
||||
|
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/~holl.../ieeefloat.html . |
|
#5
|
||||
|
||||
|
Quote:
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. |
|
#6
|
||||
|
||||
|
Quote:
![]() |
|
#7
|
||||
|
||||
|
Quote:
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > how to display individual bits of float (?) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|