By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,602 Members | 1,466 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,602 IT Pros & Developers. It's quick & easy.

convert float to binary

P: 4
how to convert * WITHOUT USING ANY FUNCTION * a float number to binary?
at least if someone can explain me how to do it it would be better then giving me the code itself.
Nov 15 '06 #1
Share this Question
Share on Google+
7 Replies


Banfa
Expert Mod 5K+
P: 8,916
You are going to have to explain what you want to do better than that.

A float is already held in a binary format.
Nov 15 '06 #2

P: 4
i will give you an example in converting an integer to binary:
the number 245 = 11110101 in binary notation.
i got the answer by algorithm that translate 245 to binary.

i want to write an algorithm for float numbers.
as i will put 12.34 and get its binary notation.

that's it.
Nov 15 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
i will give you an example in converting an integer to binary:
the number 245 = 11110101 in binary notation.
i got the answer by algorithm that translate 245 to binary.

i want to write an algorithm for float numbers.
as i will put 12.34 and get its binary notation.

that's it.
But ints and floats are 2 completely different kettle of fish. Your int (value 245) is actually held in memory in the binary notation you give 11110101.

However the binary notation of the float 12.34 is

1100.010101110000101000111101011100001010001111010 111

But 12.34 in a double variable is held in memory as

10101110 01000111 11100001 01111010
00010100 10101110 00101000 01000000

and 12.34 in a float variable is held in memory as

10100100 01110000 01000101 01000001

So the question is which one are you trying to calculate?
Nov 15 '06 #4

P: 4
i guess it's the last one:
10100100 01110000 01000101 01000001

if you please can tell me how you get to binary notation from 12.34
so i can write the correct algorithm to do it.

thanx
Nov 15 '06 #5

Expert 100+
P: 1,510
i guess it's the last one:
10100100 01110000 01000101 01000001

if you please can tell me how you get to binary notation from 12.34
so i can write the correct algorithm to do it.

thanx
Would be worth having a look at URL
http://en.wikipedia.org/wiki/IEEE_754

which describes the IEEE 754 floating point standard
Nov 15 '06 #6

Banfa
Expert Mod 5K+
P: 8,916
i guess it's the last one:
10100100 01110000 01000101 01000001

if you please can tell me how you get to binary notation from 12.34
so i can write the correct algorithm to do it.
As pseudo code it is

Expand|Select|Wrap|Line Numbers
  1. DECLARE FLOAT f
  2. SET f = 12.34
  3. DECLARE BYTE POINTER p
  4. SET p = THE ADDRESS OF f
  5.  
  6. FOR EACH BYTE IN F
  7.     PRINT THE BYTE p POINTS TO IN BINARY
  8.     SET p = p + 1
  9. END FOR
  10.  
Nov 15 '06 #7

Expert 100+
P: 1,510
i guess it's the last one:
10100100 01110000 01000101 01000001

if you please can tell me how you get to binary notation from 12.34
so i can write the correct algorithm to do it.

thanx
printing integers in binary is easy so you could have a union, e.g.
Expand|Select|Wrap|Line Numbers
  1. typedef union  {
  2.     float f;
  3.     int i;
  4. } Number;
  5.  
assign the floating point value to f and then print i in binary

you would need to check that int and float were the same size using the sizeof() operator
Nov 16 '06 #8

Post your reply

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