The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
how to display individual bits of float (?)
Discuss how to display individual bits of float (?) in the C Programming forum on Dev Shed. how to display individual bits of float (?) C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 14th, 2003, 11:30 AM
|
|
matthewdoucette.com
|
|
Join Date: May 2002
Posts: 635

Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 11
|
|
|
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
|

March 14th, 2003, 12:42 PM
|
|
Contributing User
|
|
Join Date: Jan 2002
Location: Seattle WA
Posts: 863
  
Time spent in forums: 22 sec
Reputation Power: 13
|
|
|
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.
|

March 14th, 2003, 01:55 PM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
Posts: 378

Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
|
|
|
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++.
|

March 14th, 2003, 02:00 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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 .
|

March 14th, 2003, 02:05 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: 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.
|

March 14th, 2003, 02:27 PM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
Posts: 378

Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
|
|
Quote: 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. 
|

March 14th, 2003, 02:30 PM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
Posts: 378

Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
|
|
Quote: 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).
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|