how to do comparisons of float values using bitwise operator?
Don't. Just... don't. Use ==
, or its wild and wacky neigbors >
and <
. There's even the crazy hybrids, <=
and >=
! These should cover all of your float-comparison needs.
Update: Never mind, don't use ==
. The others should be fine.
Update update: Not using ==
means you probably shouldn't use <=
or >=
, either. The moral of the story is that floats are tricksy, which is why you absolutely, definitely shouldn't be trying bitwise operations on them.
The domain represented by float
numbers doesn't fit well with their bit implementation and manipulation.
You can easily apply whatever bitwise operator on your floats but you won't obtain anything useful since these operators will modify the number in a way that simply doesn't make any sense if you want to treat them as floats..
What kind of sense would ANDing two exponents or XORing two mantissas have? I mean in practical float operations..
Floating point layout is strongly platform dependant. I remember seeing ugly hack to draw random float numbers using only integer operations, but this is definitely not portable.
I don't think using a bitwise operator on a float
will do what you think it will do. Before doing so, make sure you're familiar with the IEEE 754 standard, which governs how floating point numbers are represented internally. While this is a perfectly valid operation, it is more than likely not very useful.
What exactly are you trying to accomplish? There is likely a better way to do it.
Under the really rare circumstances that you need to do this (e.g., dealing with a foreign floating point format, such as data in VAX G-format being manipulated on a PC) you normally do it by putting the floating point data into an integer of the same size, or else into an array of char (again, of the right size).
Don't expect such code to be anywhere close to portable or clean. In a typical case, you want to do as little this was as possible, typically just read in the raw data, create the closest native floating point value possible, and work with that. Even when/if you have to deal with such foreign data, doing things like comparisons in the foreign format should still generally be avoided.
There's a good description at the link below which uses the trick to make a highly-optimized sort function for floating point numbers.
http://www.stereopsis.com/radix.html
Obviously, using this kind of hack is not portable and inadvisable under most circumstances.
If you read carefully the IEEE specs about fp numbers, you are at the start. The next step is to implement in software what is already done by the hardware if the cpu has fp support otherwise you have to implement fp operations (according to IEEE) from scratch in software... Perfectly doable both (it was what one had to do before IEEE was used into cpus or fp coprocessors, if the IEEE 754-whatever was needed).
Let us suppose that your target can't fp at all, so you need implement it from scratch. Then it is up to you to decide how to store the number in memory (you can agree with endianness of your system or not); e.g. the float for 1.23 is stored into mem as 0xA4709D3F on my machine (LE) and in fact the "right" way is 0x3F9D70A4 (our way of writing resembles more BE than LE, but there is no a "right" way... even though this way allows us to check the datum directly with specs, so that if I write -1.23, I obtain 0xBF9D70A4, where it is clear that the sign bit is raised to 1)
But since we're going to implement it from scratch we can write the number into memory this way:
unsigned char p[4];
p[0] = 0x3f; p[1] = 0x9d; p[2] = 0x70; p[3] = 0xa4;
and then it comes the hard part... Like this:
bool is_positive(float_t *p)
{
return ! (p[0] & 0x80); // or alike
}
We work in memory supposing our proc is not able to handle 32 bit (or more) integers. Of course I've picked the simpler operation...! The others are harder, but starting from the IEEE 754 description and doing some reasoning, you can implement what you want. As you see, it is not so easy... Somewhere you could find libraries that implements operations on fp numbers when there's no floating point unit, but now I was not able to find any (examples could be Amiga mathieeedoubbas.library, but I think you can't find the sources for this, and anyway it could be directly in m68k asm...; just to say that software impl could exist somewhere...)