C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 14th, 2003, 11:30 AM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
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

Reply With Quote
  #2  
Old March 14th, 2003, 12:42 PM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
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.

Reply With Quote
  #3  
Old March 14th, 2003, 01:55 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
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++.

Reply With Quote
  #4  
Old March 14th, 2003, 02:00 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,142 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 33 m 48 sec
Reputation Power: 1974
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 .

Reply With Quote
  #5  
Old March 14th, 2003, 02:05 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,142 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 33 m 48 sec
Reputation Power: 1974
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.

Reply With Quote
  #6  
Old March 14th, 2003, 02:27 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
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.

Reply With Quote
  #7  
Old March 14th, 2003, 02:30 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
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).

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > how to display individual bits of float (?)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap