473,770 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?

Thank you in advance!
L R
May 18 '07 #1
45 5310
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,
but instead tell what your actual problem is.

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
different methods and was told the advantages and disadvantages of each
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
complicate your bit manipulations.

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,
but instead tell what your actual problem is.
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
different methods and was told the advantages and disadvantages of each
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.

Similar topics

11
9028
by: Randell D. | last post by:
Why would one use bitwise operators? I can program in various languages in some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise operators before, but never understood why anyone would use them - any real world examples or ideas? Examples follow (that I am reading in my Core JavaScript Guide 1.5). 15 & 9 yields 9 (1111 & 1001 = 1001) 15 | 9 yields 15 (1111 | 1001 = 1111) 15 ^ 9 yields 6 (1111 ^ 1001 = 0110) in...
2
3469
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those with 2's complement, 1's complement, or sign-magnitude arithmetic. But the followup remark is sometimes also made that the choice of arithmetic isn't completely unconstrained, since the bitwise operators seem to presume a base-2 machine.
6
8415
by: jawilson2 | last post by:
Hello, I have some data that stores two 16-bit integers in a 32-bit float, and I would like to extract the data. I want to do something like //----------------------------------------------------------------- float *data; short *buff; int dataLen; //number of points in data //other code...
5
5798
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what you will but I cannot seem to see the purpose for bitwise operators. Especially the operators bitwise OR ( | ) and bitwise AND ( & ), I'm just not getting it. I have searched around and really haven't found anything that gave explanation to why...
4
2080
by: skorobogatov | last post by:
I can't understand a little thing in bitwise operations. In PHP and Javascript 4653896912>>13 = -212992 In Python and Ruby 4653896912>>13 = 568102 In Python and Ruby - it's ok. I understand.
4
6229
by: siryuhan | last post by:
I am trying to apply bitwise operations on float, double, and long double values. I do not believe this is possible natively, so I created a wrapper class to construct a double value given two integers, similar to the bit representation of floating point numbers. However, this is tedious and the decoding/encoding process is not efficient. Is there a portable way to apply AND, OR, XOR, >>, and << to doubles? Thanks in advance.
29
5953
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not often done in Python because of keyword arguments and dicts, which are lot more versatile. Another major use, talking to hardware, is not something oft done in Python either. It seems like this occasional usage wouldn't justify having built-in...
0
9595
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8891
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.