![]() |
| | #1 |
| Registered User Join Date: Mar 2008 Location: India
Posts: 72
| float variable displaying in hexadecimal format using pointers Is this valid what iam doing here? Code: #include<stdio.h>
int main()
{
float fVal = 10.1234;
char *ptr;
int i;
ptr = &fVal;
for(i = 0;i < 4;i++)
{
printf("%02x \n",*ptr);
ptr++;
}
return 0;
}
72 fffffff9 21 41 iam trying to find out the float data variable bit pattern , till now not with much success ( i.e how10.1234 will be converted into bits and kept in that 4 bytes).if any body can find link or tutorial that would be really benificial for me. thanks for all the help friends |
| vlrk is offline | |
| | #2 | |
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,866
| Quote:
http://en.wikipedia.org/wiki/IEEE_fl...point_standard
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 | |
| hk_mp5kpdw is offline | |
| | #3 |
| Registered User Join Date: Jun 2008
Posts: 141
| Someone I know showed me this trick a long time ago. I never entirely understood it though... Code: typedef unsigned int u32;
typedef unsigned long long int u64; //AKA int64
float tmpfloat;
u64 *castfloat=(u64*)&tmpfloat;
double tmpdouble;
u64 *castdouble=(u64*)&tmpdouble;
u64 InputValue;
tmpfloat = 100.0;
InputValue = *castfloat;
InputValue &= 0xFFFFFFFF;
printf("%08X\n", InputValue);
tmpdouble = 100.0;
InputValue = *castdouble;
printf("%I64X\n", InputValue);
|
| Viper187 is offline | |
| | #4 |
| Registered User Join Date: Sep 2008 Location: Toronto, Canada
Posts: 521
| You might want to clean up the output a little: Code: unsigned char *ptr;
...
ptr = (unsigned char *)&fVal;
...
printf("%02X \n",*ptr);
So... 72 F9 21 41 is equivalent to 01110010111110010010000101000001 Now you can start to decode each field - sign, exponent, mantissa... |
| nonoob is offline | |
| | #5 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
01000001 00100001 11111001 01110010 depending on byte order of the machine. Given the size of the number, I'd hazard a guess on the latter (1.0 I know is 3F800000, and 10.234 would be a bit bigger, but not dramatically so, since it's about 2^4 larger, which means 4121F972 is more likely than something starting with 72, near the limit of positive values, which makes the exponent large indeed). -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | |
| matsp is offline | |
| | #6 | |
| Registered User Join Date: Jun 2008
Posts: 141
| Quote:
![]() Nintendo 64 uses a lot of these. heheh | |
| Viper187 is offline | |
| | #7 |
| Registered User Join Date: Mar 2008 Location: India
Posts: 72
| These responses are indeed helping me a lot. I request you to explain me the below lines which i found in "http://en.wikipedia.org/wiki/IEEE_floating-point_standard" Code: Next, the number (without the sign; i.e., unsigned, no two's complement) is converted to binary notation, giving 1110110.101. The 101 after the binary point has the value 0.625 because it is the sum of: 1. (2 to the power of −1) × 1, from the first digit after the binary point 2. (2 to the power of −2) × 0, from the second digit 3. (2 to the power of −3) × 1, from the third digit. friends please give your thoughts. thanks for all your inputs . |
| vlrk is offline | |
| | #8 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,314
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #9 |
| Registered User Join Date: Mar 2008 Location: India
Posts: 72
| 101 = (2 power of 2) * 1+ (2 power of 1) * 0 + (2 power of 0) * 1 = 4+0+1=5 In this way |
| vlrk is offline | |
| | #10 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,314
| Good. Notice that in converting 101 in base 2 to base 10, you began by multiplying with 2^2 (2 to the power of 2, not bitwise xor), then 2^1, and finally 2^0. Now, for 0.101 in base 2, do the same process, but multiply with 2^-1, 2^-2 and 2^-3, respectively.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline | |
| | #11 |
| Registered User Join Date: Mar 2008 Location: India
Posts: 72
| Now i got that , we have to take the fractional part if iam right here. i.e 2^-1= 1/2 = 0.5 * 1 = 0.5 2^-2 = 1/4 = 0.25 * 0 = 0 2^-3 = 1/8 = 0.125 * 1 = 0.125 0.5+0+0.125=0.625 is it correct .. ? or some other way |
| vlrk is offline | |
| | #12 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,314
| Yes, that is correct.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline | |
| | #13 |
| Registered User Join Date: Mar 2008 Location: India
Posts: 72
| 0.625 = 2^-4 also so why cannot it be 1000. where as to find the binary notation for any integer we do a 2 division and the remainder becomes binary notation. similarly for this fraction part what is the method used to know the binary notation. thanks friends |
| vlrk is offline | |
| | #14 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,314
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #15 |
| Registered User Join Date: Sep 2008 Location: Toronto, Canada
Posts: 521
| I don't know where your 0.625 comes from... Let's start again. 10.1234 = 1.265425 * 2^3 The computer scales the number down by whatever power of 2 it takes to make the number 1.nnnnnn. In this case, divided it by 8. The integer '1' is tossed since it's always present after this scaling, so we don't need to waste a bit. The binary representation of 1.265425 is 1.01000011111100101110010 but you didn't see the first '1' 1 + (implicit) 0.25 + etc. |
| nonoob is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| sorting number | Leslie | C Programming | 8 | 05-20-2009 04:23 AM |
| C in UNIX environment; external bubble sort doesn't work | stn0091 | C Programming | 2 | 05-13-2009 03:25 PM |
| Reflective Factory Design | Shamino | Game Programming | 4 | 12-16-2005 06:50 PM |
| help w/another program!!! | edshaft | C++ Programming | 2 | 12-17-2001 11:34 AM |
| How do you search & sort an array? | sketchit | C Programming | 30 | 11-03-2001 05:26 PM |