convert float to binary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ayaniv
    New Member
    • Nov 2006
    • 4

    convert float to binary

    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.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You are going to have to explain what you want to do better than that.

    A float is already held in a binary format.

    Comment

    • ayaniv
      New Member
      • Nov 2006
      • 4

      #3
      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.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by ayaniv
        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.0101011100 001010001111010 111000010100011 11010111

        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?

        Comment

        • ayaniv
          New Member
          • Nov 2006
          • 4

          #5
          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

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            Originally posted by ayaniv
            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.or g/wiki/IEEE_754

            which describes the IEEE 754 floating point standard

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by ayaniv
              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

              Code:
              DECLARE FLOAT f
              SET f = 12.34
              DECLARE BYTE POINTER p
              SET p = THE ADDRESS OF f
              
              FOR EACH BYTE IN F
                  PRINT THE BYTE p POINTS TO IN BINARY
                  SET p = p + 1
              END FOR

              Comment

              • horace1
                Recognized Expert Top Contributor
                • Nov 2006
                • 1510

                #8
                Originally posted by ayaniv
                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.
                Code:
                typedef union  {
                    float f;
                    int i;
                } Number;
                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

                Comment

                Working...