Hi
i have a question:
how can we wirte a program that can print the 2 first bytes of a double ??
Hi
i have a question:
how can we wirte a program that can print the 2 first bytes of a double ??
Can you clearify that, maybe give an example. And then try it on your own so we have some code work with. No one wants to write the program for you.
Don't quote me on that... ...seriously
I don't want anyone to write a program for me !
i just needed help thats all !
What about Mod division or something similar?
You can cast the field as something else to print the first 2 bytes, either with a (cast) operator or perhaps code up a union to redefine it.
Todd
Depends what you mean by the first two bytes. Doubles are generally not guarenteed to be kept in the same binary format that an integer is. You also run into issues with endianness.
If you don't care about any of that and just want to print it for the heck of it, then Todd's suggestion of a union is probably a good idea. The same can manually be done without a union, but a union takes care of some pointer and casting details.
Here's something with the union idea:
Disclaimer: This is very messy, non-portable (in the sense that you may receive different numerical results on different systems), and possibly has a few errors in it (since I rushed it).Code:#include <stdio.h> #include <string.h> typedef union { char asChar; short asShort; int asInt; long asLong; double asDouble; }doublewrapper; void dumpWrapper(const doublewrapper * const dw); int main(void) { doublewrapper dw; memset(&dw, 0, sizeof(doublewrapper)); printf("Enter a double: "); fflush(stdout); scanf("%lf", &dw.asDouble); dumpWrapper(&dw); return 0; } void dumpWrapper(const doublewrapper * const dw) { printf("Char\t\tShort\t\tInt\t\tLong\t\tDouble\n\n"); printf("%08d\t%08d\t%08d\t%08ld\t%f\n", dw->asChar, dw->asShort, dw->asInt, dw->asLong, dw->asDouble); printf("%08X\t%08X\t%08X\t%08lX\t%f\n", dw->asChar, dw->asShort, dw->asInt, dw->asLong, dw->asDouble); }
It might be a good idea to use unsigned chars, because ordinary chars can be signed, and the values dealt with might easily be negative when represented as signed chars. Unless you don't mind negative byte values, in which case you might want to use signed chars specifically, because ordinary chars can be signed or unsigned.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
bit masking could do as well i suppose
ssharish
thanks Guys!
And of course without knowing at least the byteorder of the architecture, you have no idea if you are displaying the least significant digits or the exponent and sign bytes of the double.
The whole excercise is pretty meaningless unless it's for the purpose of understanding unions/pointer casts.
--
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.