473,760 Members | 10,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Floating point number to binary

Hi,

I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help me
out with it?

Thanks
Gaurav
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et
Nov 14 '05
25 3653
Gaurav Verma wrote:

Hi,

I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help me
out with it?


float MyNum = 123.456;

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et
Nov 14 '05 #21
"Gaurav Verma" <ve**********@g mail.com> wrote in message
news:cl******** ********@pletho ra.net...
I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help me
out with it?


Here's a simple program that does it for Digital Mars C:

#include <stdio.h>

int main()
{
double d = 123.456;
printf("%032b %032b\n", ((int*)&d)[0], ((int*)&d)[1]);
return 0;
}

which prints:

000110101001111 110111110011101 11 010000000101111 011011101001011 11

Of course, it relies on some non-standard behavior, like the %b format for
binary, it assumes that sizeof(double)= =2*sizeof(int)= =64 bits, and there
are endianness issues. But this should help in seeing how it might be done.

-Walter
www.digitalmars.com free C/C++/D compilers
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et
Nov 14 '05 #22
jmh
Gaurav Verma wrote:
Hi,

I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help me
out with it?

Thanks
Gaurav


One approach wuold be to split the number into it's integer
and it's fractional parts, use the division-remainder method
to convert the integer part to a string representatin of the
binary value (i.e., case all the 1s and 0s as char and then
contatinate them) they apply the mulitple-remainder (is that
what it's called?) method to the frational part and do the
same casts, then concatinate the two parts together.
jmh
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et
Nov 14 '05 #23
Nicolas Pavlidis wrote:
unsigned char comp_mask = 0x1 << ((sizeof(char) * 8) -1);
char* bin_rep = malloc(sizeof(c har) * sizeof(char) * 8 + 1);


Those statements won't do what you want, unless CHAR_BIT equals 8.
sizeof(char) always equals 1, so it's a little strange to use
sizeof(char) as a multiplicand in an expression.
If 8 is replaced by CHAR_BIT, then the 0x1 expression, would
also have to be replaced by one with an unsigned type, like 1u.

--
pete
Nov 14 '05 #24
pete <pf*****@mindsp ring.com> writes:
Nicolas Pavlidis wrote:
unsigned char comp_mask = 0x1 << ((sizeof(char) * 8) -1);
char* bin_rep = malloc(sizeof(c har) * sizeof(char) * 8 + 1);


Those statements won't do what you want, unless CHAR_BIT equals 8.
sizeof(char) always equals 1, so it's a little strange to use
sizeof(char) as a multiplicand in an expression.
If 8 is replaced by CHAR_BIT, then the 0x1 expression, would
also have to be replaced by one with an unsigned type, like 1u.


It was code I wrote for some assignement on a university course.
At that time I didn't know that sizeof(char) == 1, on all platforms,
CHAR_BIT would be better here.

But I had no problems on 32bit Linux.

Kind regards,
Nicolas
--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| pa****@sbox.tug raz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Nov 14 '05 #25
On 16 Sep 2004 06:32:19 GMT, in comp.lang.c , ve**********@gm ail.com
(Gaurav Verma) wrote:
Hi,

I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help me
out with it?


It will almost certainly be pointed out to you by many people that all
numbers in C are already in binary format (unless you're one of the rare
people with a nonbinary computer).

I expect you mean you want to print out the bitpattern of a floating point
value. Thats not really a C question, its a question about how to translate
the floating point format for your hardware.

Once you know, that, simply iterate over the bits of the number, printing a
1 for each set bit and a 0 for each unset bit.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #26

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

Similar topics

4
7852
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a long int to store the value, and the precision (number of decimal points) is variable (it's a templated class): template <size_t _decimal_places = 4> class FixedFloat {
3
1688
by: Ketan Parikh | last post by:
Hey everybody, I am new to this group. I am from India. Consider the following code. main() { float k=0.7; if (k < 0.7)
687
23646
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't believe that. in pieces of the application where speed really matters you can still use "normal" functions or even static methods which is basically the same. in C there arent the simplest things present like constants, each struct and enum...
3
3799
by: Mark L Pappin | last post by:
<puts on Compiler Vendor hat> I've recently discovered that our compilers don't make any attempt to handle floating point overflow in add/subtract/ multiply/divide, with the result that programmers who don't range- check their data can e.g. multiply two very tiny values and end up with a very large one. This is (AFAICT) quite fine according to the Standard but is certainly a QoI issue and I'd like to raise ours. I believe that it's...
10
4186
by: 63q2o4i02 | last post by:
Hi, I'm using python to run some lab equipment using PyVisa. When I read a list of values from the equipment, one of the fields is 32 bits of flags, but the value is returned as a floating point number, either in ASCII format, or pure binary. In either case, since I'm using PyVisa, it converts the number to a single precision floating point, and that's what I have to work with. The question is how do I recover the bits out of this...
9
3521
by: Steven D'Aprano | last post by:
I'm looking for some way to get the next floating point number after any particular float. (Any mathematicians out there: I am aware that there is no "next real number". But floats are not real numbers, they only have a finite precision.) According to the IEEE standard, there should be a routine like next(x,y) which returns the next float starting from x in the direction of y.
8
6235
by: vendredi5h | last post by:
Hello all, I'd like to split a 64-bits word (hold as a floating point number) into two 32-bits words (hold as integers). Bitwise operators are working on Integers, not on floating point. Anyone can tell me how I can do this? Yannick
33
2770
by: dis_is_eagle | last post by:
hi....i have encountered strange problem regarding floating point comparison...the problem is... main() { float a=0.7; if(0.7 a) printf("hi"); else printf("hello");
15
8061
by: Mukesh_Singh_Nick | last post by:
Why does floating point have a rounding error? How to work around it? For example, the following: flaot f = 1234.12345678F; printf("%2f\n", f) //prints 1234.123413 and
0
10107
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...
0
9945
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9900
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
9765
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
8768
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...
0
5214
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
5361
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3442
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2733
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.