473,699 Members | 2,165 Online

# bitwise on float

Hi!
I now that I can't do straight forward any bitwise operation on float
(double etc..). But I wondering what is the easiest/best way to do this?
I was thinking if I have float x=1.1111 so I can multiple it by 1000 to
get 11111 and the preform bitwise like <<2 to get 88888 and then divide
by 1000 to go back to float 8.8888. but these seem like "nasty" way to
do it. So maybe some of you have great tips?

L R
May 18 '07 #1
45 5284
On May 18, 7:15 am, Carramba <u...@example.n etwrote:
Hi!
I now that I can't do straight forward any bitwise operation on float
(double etc..). But I wondering what is the easiest/best way to do this?
I was thinking if I have float x=1.1111 so I can multiple it by 1000 to
get 11111 and the preform bitwise like <<2 to get 88888 and then divide
by 1000 to go back to float 8.8888. but these seem like "nasty" way to
do it. So maybe some of you have great tips?
If you just want to do math with it, then all of that is a complete
waste of time.
If you want to understand what is going on with the internal format,
then take a look at:

7.12.6.4 The frexp functions
Synopsis
1 #include <math.h>
double frexp(double value, int *exp);
float frexpf(float value, int *exp);
long double frexpl(long double value, int *exp);
Description
2 The frexp functions break a floating-point number into a normalized
fraction and an integral power of 2. They store the integer in the int
object pointed to by exp.
Returns
3 If value is not a floating-point number, the results are
unspecified. Otherwise, the frexp functions return the value x, such
that x has a magnitude in the interval [1/2, 1) or zero, and value
equals x * 2^*exp. If value is zero, both parts of the result are
zero.
7.12.6.6 The ldexp functions
Synopsis
1 #include <math.h>
double ldexp(double x, int exp);
float ldexpf(float x, int exp);
long double ldexpl(long double x, int exp);
Description
2 The ldexp functions multiply a floating-point number by an integral
power of 2. A range error may occur.
Returns
3 The ldexp functions return x * 2^exp.

May 18 '07 #2
user923005 skrev:
On May 18, 7:15 am, Carramba <u...@example.n etwrote:
>Hi!
I now that I can't do straight forward any bitwise operation on float
(double etc..). But I wondering what is the easiest/best way to do this?
I was thinking if I have float x=1.1111 so I can multiple it by 1000 to
get 11111 and the preform bitwise like <<2 to get 88888 and then divide
by 1000 to go back to float 8.8888. but these seem like "nasty" way to
do it. So maybe some of you have great tips?

If you just want to do math with it, then all of that is a complete
waste of time.
neither or... as I stated earlier I need to perform bitwise operation on
float. right now I program works only on integer values, and thus
precision is "ok". but in order to get really nice precision I need
floats, and bitwise operations on them :)
If you want to understand what is going on with the internal format,
then take a look at:

7.12.6.4 The frexp functions
Synopsis
1 #include <math.h>
double frexp(double value, int *exp);
float frexpf(float value, int *exp);
long double frexpl(long double value, int *exp);
Description
2 The frexp functions break a floating-point number into a normalized
fraction and an integral power of 2. They store the integer in the int
object pointed to by exp.
Returns
3 If value is not a floating-point number, the results are
unspecified. Otherwise, the frexp functions return the value x, such
that x has a magnitude in the interval [1/2, 1) or zero, and value
equals x * 2^*exp. If value is zero, both parts of the result are
zero.
7.12.6.6 The ldexp functions
Synopsis
1 #include <math.h>
double ldexp(double x, int exp);
float ldexpf(float x, int exp);
long double ldexpl(long double x, int exp);
Description
2 The ldexp functions multiply a floating-point number by an integral
power of 2. A range error may occur.
Returns
3 The ldexp functions return x * 2^exp.
May 18 '07 #3
In article <46************ ***********@new s.luth.se>,
Carramba <us**@example.n etwrote:
>user923005 skrev:
>On May 18, 7:15 am, Carramba <u...@example.n etwrote:
>>Hi!
I now that I can't do straight forward any bitwise operation on float
(double etc..). But I wondering what is the easiest/best way to do this?
I was thinking if I have float x=1.1111 so I can multiple it by 1000 to
get 11111 and the preform bitwise like <<2 to get 88888 and then divide
by 1000 to go back to float 8.8888. but these seem like "nasty" way to
do it. So maybe some of you have great tips?

If you just want to do math with it, then all of that is a complete
waste of time.

neither or... as I stated earlier I need to perform bitwise operation on
float. right now I program works only on integer values, and thus
precision is "ok". but in order to get really nice precision I need
floats, and bitwise operations on them :)
What Are You Really Trying To Do?

Whatever it is, trying to do bitwise operations on floats is probably
the wrong way to do it.
dave

--
Dave Vandervies dj******@csclub .uwaterloo.ca
If you wish to send gifts, just provide me with your banking information,
and I'll select something suitable.
--Eric Schwartz in the scary devil monastery
May 18 '07 #4
In article <46************ ***********@new s.luth.se>,
Carramba <us**@example.n etwrote:
>user923005 skrev:
>On May 18, 7:15 am, Carramba <u...@example.n etwrote:
>>Hi!
I now that I can't do straight forward any bitwise operation on float
(double etc..). But I wondering what is the easiest/best way to do this?
I was thinking if I have float x=1.1111 so I can multiple it by 1000 to
get 11111 and the preform bitwise like <<2 to get 88888 and then divide
by 1000 to go back to float 8.8888. but these seem like "nasty" way to
do it. So maybe some of you have great tips?

If you just want to do math with it, then all of that is a complete
waste of time.

neither or... as I stated earlier I need to perform bitwise operation on
float. right now I program works only on integer values, and thus
precision is "ok". but in order to get really nice precision I need
floats, and bitwise operations on them :)
And like the original responder said, attempting to do bitwise manipulation of
floating point number can't be done.

Now with that said, there are intergral types available in C that have more
bits that may allow you to get the extra precision that you imply you need.
take a look at the long, and long long types.

One thing you haven't mentioned and you should is WHY you are performing
bitwise operations. If you tell us what you are attempting to acomplish,
then we might be able to help you better. And DON'T say that what you need
to do is perform bitwise manipulation of floating point numbers. Do not
simply ask for help on what you believe to be a step in solving your problem,

I can illustrate this by something that happened on USENET a long time ago.
Someone asked "How do I sort a list of numbers?" This person received many
method. Eventually in the thread, someone asked the question "Why do you
want to sort the list of numbers?" He answered "Because I need to find the
smallest number in the list. So if I sort it, I can simply pick the 1st number
in the list after sorting."

So yes, sorting a list of numbers could solve the original posters problem,
but obviously the solution that the poster came up with was sub-optimal to
say the least.
May 18 '07 #5
In article <46************ ***********@new s.luth.se>,
Carramba <us**@example.n etwrote:
>I now that I can't do straight forward any bitwise operation on float
(double etc..). But I wondering what is the easiest/best way to do this?
Not to.
>I was thinking if I have float x=1.1111 so I can multiple it by 1000 to
get 11111 and the preform bitwise like <<2 to get 88888 and then divide
by 1000 to go back to float 8.8888. but these seem like "nasty" way to
do it. So maybe some of you have great tips?
floats might not happen to be the same size as an integral type,
so if you are absolutely determined to do bitwise operations on them,
you will have to use something like,

float_as_bits_p = (unsigned char *)&the_float;

to gain access to the bits. This will give you an object of
length sizeof the_float but the bit ordering within that array
will be completely undefined. Do not use plain or signed char
for this purpose: plain or unsigned char can have 'trap bits'
that unsigned char is guaranteed not to have.

The other posters referred you to
functions that break apart doubles into exponent and mantissa;
what they didn't note (because it is outside the scope of C proper)
is that in IEEE 754 there is a "hidden bit" in the binary
representation: because the normalized mantissa will always start
with binary 1, the binary 1 will not actually be stored. This will

There are no C operators to do << or + or | or whatever across
entire arrays of unsigned char, so you will have to do each char
one at a time and "manually" propogate carry bits and borrows and
rolling in to the next bucket and so on.

Don't forget to manually re-normalize the numbers before
constructing the final float. Oh, and watch out because neither
the exponent nor the mantissa are two's complement.

If any of this isn't making immediate sense to you, chances are
good that you shouldn't attempt to do this.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
May 18 '07 #6
On 2007-05-18 12:18:19 -0700, Carramba <us**@example.n etsaid:
user923005 skrev:
>On May 18, 7:15 am, Carramba <u...@example.n etwrote:
>>Hi!
I now that I can't do straight forward any bitwise operation on float
(double etc..). But I wondering what is the easiest/best way to do this?
I was thinking if I have float x=1.1111 so I can multiple it by 1000 to
get 11111 and the preform bitwise like <<2 to get 88888 and then divide
by 1000 to go back to float 8.8888. but these seem like "nasty" way to
do it. So maybe some of you have great tips?

If you just want to do math with it, then all of that is a complete
waste of time.

neither or... as I stated earlier I need to perform bitwise operation
on float.
No, you don't. That is, "perform bitwise operations on float" cannot be
your ultimate goal. You *think* that it is a step required to reach
your goal (whatever it is), but I can assure you that it is not. What
do you *really* want to do?
right now I program works only on integer values, and thus precision is
"ok". but in order to get really nice precision I need floats, and
bitwise operations on them :)
You said that you wanted to start with 1.1111 and get 8.8888. What's
wrong with:

new = old * (1<<2);

?

--
Clark S. Cox III
cl*******@gmail .com

May 18 '07 #7
Clark Cox said:
On 2007-05-18 12:18:19 -0700, Carramba <us**@example.n etsaid:
>user923005 skrev:
>>On May 18, 7:15 am, Carramba <u...@example.n etwrote:
Hi!
I now that I can't do straight forward any bitwise operation on
float (double etc..). But I wondering what is the easiest/best way
to do this? I was thinking if I have float x=1.1111 so I can
multiple it by 1000 to get 11111 and the preform bitwise like <<2
to get 88888 and then divide by 1000 to go back to float 8.8888.
but these seem like "nasty" way to do it. So maybe some of you have
great tips?
<snip>
>
You said that you wanted to start with 1.1111 and get 8.8888. What's
wrong with:

new = old * (1<<2);

?
Only the fact that 1.1111 * (1<<2) is 4.4444. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 18 '07 #8
In article <20070518125133 16807-clarkcox3@gmail com>,
Clark Cox <cl*******@gmai l.comwrote:
>You said that you wanted to start with 1.1111 and get 8.8888. What's
wrong with:
>new = old * (1<<2);
Other than the fact that that would give ~4.4444 ? :)
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
May 18 '07 #9
John Cochran skrev:
In article <46************ ***********@new s.luth.se>,
Carramba <us**@example.n etwrote:
>user923005 skrev:
>>On May 18, 7:15 am, Carramba <u...@example.n etwrote:
Hi!
I now that I can't do straight forward any bitwise operation on float
(double etc..). But I wondering what is the easiest/best way to do this?
I was thinking if I have float x=1.1111 so I can multiple it by 1000 to
get 11111 and the preform bitwise like <<2 to get 88888 and then divide
by 1000 to go back to float 8.8888. but these seem like "nasty" way to
do it. So maybe some of you have great tips?
If you just want to do math with it, then all of that is a complete
waste of time.
neither or... as I stated earlier I need to perform bitwise operation on
float. right now I program works only on integer values, and thus
precision is "ok". but in order to get really nice precision I need
floats, and bitwise operations on them :)

And like the original responder said, attempting to do bitwise manipulation of
floating point number can't be done.

Now with that said, there are intergral types available in C that have more
bits that may allow you to get the extra precision that you imply you need.
take a look at the long, and long long types.

One thing you haven't mentioned and you should is WHY you are performing
bitwise operations. If you tell us what you are attempting to acomplish,
then we might be able to help you better. And DON'T say that what you need
to do is perform bitwise manipulation of floating point numbers. Do not
simply ask for help on what you believe to be a step in solving your problem,
Some time it's strange that one can't by taken by word and believed ...
I'm working with genetic algorithms, and have implemented it in C. in
order to work one need to perform crossover and bitflip operations on
encoded data.
crossover - take two values represented by binary string and, "cast a
coin" and exchange part of those strings -bitmask operation
bitflip - cast a coin and flip bit in that place.

reason one:
since this is not forum for genetic algorithms I didn't bother to
explain earlier, and it would take more than this little peace to
explain in details what I'm doing
reason two:
because in the I have been shouted and treat malicious then I have
posted something that wasn't related to ansi c. and underlying problem
isn't so I just posted only what are my intentions to do.

I can illustrate this by something that happened on USENET a long time ago.
Someone asked "How do I sort a list of numbers?" This person received many
method. Eventually in the thread, someone asked the question "Why do you
want to sort the list of numbers?" He answered "Because I need to find the
smallest number in the list. So if I sort it, I can simply pick the 1st number
in the list after sorting."
there are not only idiots that post on this news group ...
So yes, sorting a list of numbers could solve the original posters problem,
but obviously the solution that the poster came up with was sub-optimal to
say the least.
May 18 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.