473,695 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to check each bit of a float

2 New Member
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 2868
drhowarddrfine
7,435 Recognized Expert Expert
Just i=&f. You can have any number of pointers point to a variable.
Mar 4 '07 #2
AdrianH
1,251 Recognized Expert Top Contributor
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_c ast<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
geliangwei
2 New Member
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 Recognized Expert Top Contributor
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 Top Contributor
Almost always more than one way to skin a cat.....
Mar 4 '07 #6
drhowarddrfine
7,435 Recognized Expert Expert
**sigh**

In assembly it's:

mov esi,[address of float]

Life was so much easier then. :)
Mar 4 '07 #7
AdrianH
1,251 Recognized Expert Top Contributor
**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
2411
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); long double sinl(long double x);
7
13988
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]) and the index is the location after "." in name. First, i should check the given name is a valid float-point, and then, to check the number in name in index locaion is > 0 or not? eg:
1
2447
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 "valid". For my circumstances, I don't consider float.PositiveInfinity valid, nor do I consider the two numbers valid if the second number is smaller than the first. In other words, I want to check that the range is valid. I notice that while...
18
2747
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 from the experts. I feel there's a tradeoff between clear, easily readdable and extensible code on one side, and safe code providing early errors and useful tracebacks on the other. I want both! How do you guys do it? What's the pythonic way? Are...
1
2781
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
3226
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 check on that value. Now I doubt if this is a safe approach and ask for your advice. Thanks a lot in advance. /G
5
3027
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 method within that class to calculate the weekly pay. Once stop is entered as the employee name, the application should terminate. Make sure the code is readable and well documented. This is what I have done is this correct, as per the parameters...
3
8044
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 check that? Thanks in advance.
10
4532
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 integer, it should return false, but it should work normally if the parameter is a float. To illustrate this, I attached a minimal example.
0
8649
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
8586
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
9137
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8864
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8838
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
4351
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
4594
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3025
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
3
1986
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.