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!
7 2853
Just i=&f. You can have any number of pointers point to a variable.
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
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?
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
Almost always more than one way to skin a cat.....
**sigh**
In assembly it's:
mov esi,[address of float]
Life was so much easier then. :)
**sigh**
In assembly it's:
mov esi,[address of float]
Life was so much easier then. :)
That would be 8x86 assembly. :p
Sign in to post your reply or Sign up for a free account.
Similar topics
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);...
|
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])
...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |