| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
C++ float
Hi,
I have a strange question. I have a bit representation of a floating point number. Now I want to make a float variable wich has value of that representaion. Since C++ does not have bitwise ops for float number, I coudl not do it that way. Coudl somebody show me a way? Thank you. |
|
#2
|
|||
|
|||
|
How is your bit representation stored? Have you tried casting it it to a float ie. (float)bit_rep?
Not entirely sure how I'd do this myself. If you figure it out before someone else gives you the answer please post it here, I'd be interested to know. -KM- |
|
#3
|
||||
|
||||
|
Could you give me a little bit more of an explination of what you are trying to do? What program is this for, what operations will it be performing, things like that.. Your statement is a little too general for me to really understand what you need.. Do you have any sample pseudocode or any code that you've worked on so far to share?
|
|
#4
|
|||
|
|||
|
The bit representation is a string. Obviously I cannot cast the string to a float.
Someone told me that I could use union. It works but union seems dangerous sometimes so I would like a more elegant solution. Sorry to get back late. |
|
#5
|
|||
|
|||
|
To get clearer, this is the union method a person told me.
Code:
String s="10001000100010001000100010001000";
union {float f;char byte[4]} bit;
bit.byte[0]=bit.byte[1]=bit.byte[2]=bit.byte[3]=1<<3; //1<<3 ="1000"
printf("%f",bit.float);
|
|
#6
|
|||
|
|||
|
This could be the way you're looking for:
Code:
BYTE bytes[sizeof(float)]; ... float* floatPtr = reinterpret_cast<float*>(bytes); Regards, hlina |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > C++ float |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|