473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to check each bit of a float

The 32-bit float number is defined by IEEE 754.
Given a float variable, f, how to check whether each of the 32 bits is '1' or '0'?

One possible solution is access f as a 32-bit int.
That is define an integer i that accesses the same memory address where f is stored.
Then, I can right shift i by one bit for 32 times and check whether the least significant bit is '1' or '0'.
But how to let i and f share the same memory address?

Thank you!
Mar 4 '07 #1
7 2853
drhowarddrfine
7,435 Expert 4TB
Just i=&f. You can have any number of pointers point to a variable.
Mar 4 '07 #2
AdrianH
1,251 Expert 1GB
Just i=&f. You can have any number of pointers point to a variable.
Well, you do need to cast it.

unsigned int i=reinterpret_cast<unsigned int&>(f);

or if using C:
unsigned int* pI = (unsigned int*)&f;

and then dereference pI using the unary * operator. I.e.

*pI>>shiftValue

Hope this helps.


Adrian
Mar 4 '07 #3
Thank you very much for your replies!

I found another solution from Internet just now by using 'union'

union four_byte
{
int i;
float f;
} share;

share.f = 2.34;

Then, I can access share.i as an integer. This seems easier.
Is that ok?
Mar 4 '07 #4
AdrianH
1,251 Expert 1GB
Thank you very much for your replies!

I found another solution from Internet just now by using 'union'

union four_byte
{
int i;
float f;
} share;

share.f = 2.34;

Then, I can access share.i as an integer. This seems easier.
Is that ok?
Yeah, that is fine since int and float are both usually 4 bytes in size.

Good going finding another solution. I had actually forgoten about that one.


Adrian
Mar 4 '07 #5
DeMan
1,806 1GB
Almost always more than one way to skin a cat.....
Mar 4 '07 #6
drhowarddrfine
7,435 Expert 4TB
**sigh**

In assembly it's:

mov esi,[address of float]

Life was so much easier then. :)
Mar 4 '07 #7
AdrianH
1,251 Expert 1GB
**sigh**

In assembly it's:

mov esi,[address of float]

Life was so much easier then. :)
That would be 8x86 assembly. :p
Mar 4 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Lodewijk Smit | last post by:
Hi, In the C standard of 1999 additional mathematical functions are added. For example, beside the traditional double sin(double x); there are also the functions: float sinf(float x);...
7
by: zjut | last post by:
I need to implement the method : round(String name, int index) The given string maybe the every type of float type, ( the msdn given the regax is that : integral-digits]exponential-digits]) ...
1
by: illegal.prime | last post by:
Hey all, I have an app, that could take two numbers of any type of numerical type int, long, double, float, uint, ulong, etc. I want to check that the numbers are part of a range that I consider...
18
by: Joel Hedlund | last post by:
Hi! The question of type checking/enforcing has bothered me for a while, and since this newsgroup has a wealth of competence subscribed to it, I figured this would be a great way of learning...
1
by: mosesdinakaran | last post by:
Hi, Is there a way to check weather a variable contains serialized data or not, as we hve some functions like ( is_ double,is_ float) to check the integer and float value. Moses
4
by: Giff | last post by:
Hi, I have a function that takes in a float and then performs a division. Since I know that it is impossible to check if the value I pass is equal to zero (being a float), I don't perform any...
5
by: hammer45 | last post by:
Modify payroll program so that it uses a class to store and retrive the employee's name, hourly rate and the number of hours worked. Use a constructor to initialize the employee information, and a...
3
by: Cheer | last post by:
Hello again, I would like to ask how could I check if the input is right: the program asks user to input a float from a keybord, but what if user inputs a char. Is there any funcion which can...
10
by: Matthias | last post by:
Dear newsgroup. I want to write a template function which accepts either integer or floating point numbers. If a certain result is not a whole number and if the template parameter is an...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.