I am trying to convert a non IEEE 754 FP value to 754 where the bit pattern representation is slightly different.
How can I get C program access to the various bits and/or bytes of the FP value?
Thanks,
Printable View
I am trying to convert a non IEEE 754 FP value to 754 where the bit pattern representation is slightly different.
How can I get C program access to the various bits and/or bytes of the FP value?
Thanks,
Probably something like
Mess about with the bits in both.u_val and read out the float in both.f_valCode:union {
float f_val;
unsigned long int u_val;
} both;
Or trick the compiler...
Then you can use bitwise operators with the var_bits variable, if float size is the same as int... which is by standard.Code:float var = ...;
int var_bits = *(int*)&var;
The union on Salem's post is also a perfectly valid option...
Are you sure?Quote:
Originally Posted by xErath
If you're messing with the bits, you can use bitfields:
Code:union MyUnion
{
float theFloat;
struct Bits
{
int a : 1;
int b : 1;
//... one variable for each bit
}theBits;
};
...Quote:
Originally Posted by moi
Its rare to find anyone with a non-32 bit computer, and non-x86 compatible compiler...
A very dissapointing line of reasoning.Quote:
Originally Posted by xErath
I think his query refers to your assertion that it is 32 bits 'by standard' ;) It is only guaranteed to be >= short and <= long. What if mkh goes on to develop programs for other platforms in the future?
Bah you guys...!!!
Nowadays most of computers ARE 32bit!!!!!
Most 32 bit compilers use 32 bit long ints, and 32 bit long floats...
Who said it'll be always like this?
Was I clear??.... :o :o :o :o :o :o :o
> Nowadays most of computers ARE 32bit!!!!!
So what if you're mostly right (which is doubtful), it won't be too long before your assumtion will be false. 64bit is emerging at the high end, and sooner or later it will work its way down to your desktop.
There are vastly more embedded processors in the world, many of which do not fit your standard assumption of a 32 bit world.
Best long term plan is to not get too "chummy" with this idea that everything is 32 bit.
Most doesn't mean 100%, 2# 64 bit computers are 'emerging', but have not yet emerged.Quote:
Originally Posted by Salem
And standard was a bad word... I mean default.
What do you mean by "not yet emerged"? When I can buy an AMD64 based computer at my local Best Buy, I'd say they're more than "emerging".Quote:
Originally Posted by xErath
Anyway, yes, when you say the word "standard" here, we pedantics think you mean The Standard(TM). ;)
Quzah.
The Ten Commandments for C Programmers #10 "All the world's a VAX"::pQuote:
This particular heresy bids fair to be replaced by "All the world's a Sun" or "All the world's a 386" (this latter being a particularly revolting invention of Satan), but the words apply to all such without limitation. Beware, in particular, of the subtle and terrible "All the world's a 32-bit machine", which is almost true today but shall cease to be so before thy resume grows too much longer.
Can anyone enlighten me? Why tihis... this crap prints 12? It seems to have something to do with long long, because Single Precision analog did work perfectly. I use VC.NET 2003
Code:#include <stdio.h>
#pragma pack(push, 1)
typedef union
{
double dValue;
struct
{
unsigned long long fraction:52;
unsigned int exponent:11;
unsigned int sign:1;
} sValue;
} DP_FPN;
#pragma pack(pop)
#define PRINT(x, type) printf(#x " = %" #type "\n", x);
int main(void)
{
PRINT(sizeof(DP_FPN), d);
return 0;
}
Read my post in this thread.
Quzah.