[.net] Extracting mantissa out of float
Hi,
in C# how can i get the mantissa bit pattern from a float
uint a = (f & 0x007fffff);
wont compile because operator & cant be applied to a float.
Cheers
Yratelev
Converting a float to an int by preserving bit pattern:
Now you can just use the bitwise stuff to get whichever part of the float you want.
[EDIT] Oh, it's C#...eh, not sure if what I wrote is valid without using an unsafe block.
float f = (some float number);unsigned int i = *(int*) &f
Now you can just use the bitwise stuff to get whichever part of the float you want.
[EDIT] Oh, it's C#...eh, not sure if what I wrote is valid without using an unsafe block.
yeh i knew how a float was constructed, just needed to know how to extract its bit pattern, thnx now!
Yratelev
Yratelev
Quote:Original post by Promit
Converting a float to an int by preserving bit pattern:float f = (some float number);unsigned int i = *(int*) &f
Now you can just use the bitwise stuff to get whichever part of the float you want.
[EDIT] Oh, it's C#...eh, not sure if what I wrote is valid without using an unsafe block.
wouldn't it be safer to use a union of a float and an int?
Quote:Original post by capn_midnightQuote:Original post by Promit
Converting a float to an int by preserving bit pattern:float f = (some float number);unsigned int i = *(int*) &f
Now you can just use the bitwise stuff to get whichever part of the float you want.
[EDIT] Oh, it's C#...eh, not sure if what I wrote is valid without using an unsafe block.
wouldn't it be safer to use a union of a float and an int?
C# doesn't have unions. A list of C# keywords is here. You can use the FixedLayout attribute to emulate them, though.
float f=...;int i = System.BitConverter.ToInt32( System.BitConverter.GetBytes(f) , 0) & 0x007fffff;
Quote:Original post by RipTornfloat f=...;int i = System.BitConverter.ToInt32( System.BitConverter.GetBytes(f) , 0) & 0x007fffff;
That looks good but will it be much slower than the unsafe version?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement