Welcome to Dream.In.Code
Become a Java Expert!

Join 149,582 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,775 people online right now. Registration is fast and FREE... Join Now!




floating points in binary

 
Reply to this topicStart new topic

floating points in binary

jack85
17 Oct, 2007 - 09:48 AM
Post #1

New D.I.C Head
*

Joined: 28 Sep, 2007
Posts: 34


My Contributions
A guys I need a function that when you are inputing for example 5.375, the binary output should be 0000010101100000. 00000101 for number 5 and 01100000 for 0.375. please I need your help to create my function.

Here is also my program:

CODE

import java.io.*;
public class BynaryFloat
{
    
    /** Creates a new instance of BynaryFloat */
    public BynaryFloat()
    {
    }
            

    public static void main(String[] args)throws IOException
    {  
    InputStreamReader reader = new InputStreamReader(System.in);
    BufferedReader input = new BufferedReader(reader);
    System.out.println("What do you want the program to do?");
    System.out.println(" Decimal to binary ... 1");
    System.out.println(" Binary to decimal ... 2");
    String choice = input.readLine();
    float c = Integer.parseInt(choice);

    if(c==1)
    {
       System.out.println(" Enter a decimal number:");
       String dba=input.readLine();
       int db = Integer.parseInt(dba);
       System.out.println(db);
       int [] m = new int [10];
       int j;
        for(j=8;j>=1;j--)
           {
                  m[j] = db%2;
              db = db/2;
           }
                float sc = 0;
                  for(j=1;j<=8;j++)
            {
               if(m[j]==0)
                 {
                               if(sc==0)
                                    {
                                       continue;
                                    }
                 }
                        System.out.print(+m[j] + " ");
                        sc = sc+1;
                        }
    }

   if(c==2)
   {
      System.out.println("Enter a binary number");
      String bda=input.readLine();
      float l=bda.length()-1;
      double sum =0;
       for(int w =0;w<bda.length();w++)
       {
              char r = bda.charAt(w);
              String str = new Character®.toString();
          float er = Integer.parseInt(str);
          sum = sum + (er*Math.pow(2,l));
              l--;
          }
        String zero = "";
        for (int i = 0; i < 16 - bda.length(); i++)
        {
            zero = zero + "0";
        }
        bda = bda + zero;
        
       System.out.println("The decimal is : "+sum);
   }

  
}
    private static String Zeroes(String bda)
    {
        String zero = "";
        for (int i = 0; i < 16 - bda.length(); i++)
        {
            zero = zero + "0";
        }
        bda = bda + zero;
        return bda;
    }
}

User is offlineProfile CardPM
+Quote Post

1lacca
RE: Floating Points In Binary
17 Oct, 2007 - 12:55 PM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Could you explain what is causing you problems? Do you get an error? Do you have a specific thing that you can't program? Do you get an error message? Do you get an unexpected output (if so please specify the input, the actual output and the expected output)?
User is offlineProfile CardPM
+Quote Post

jack85
RE: Floating Points In Binary
17 Oct, 2007 - 03:16 PM
Post #3

New D.I.C Head
*

Joined: 28 Sep, 2007
Posts: 34


My Contributions
QUOTE(jack85 @ 17 Oct, 2007 - 10:48 AM) *

A guys I need a function that when you are inputing for example 5.375, the binary output should be 0000010101100000. 00000101 for number 5 and 01100000 for 0.375. please I need your help to create my function.

Here is also my program:

CODE

import java.io.*;
public class BynaryFloat
{
    
    /** Creates a new instance of BynaryFloat */
    public BynaryFloat()
    {
    }
            

    public static void main(String[] args)throws IOException
    {  
    InputStreamReader reader = new InputStreamReader(System.in);
    BufferedReader input = new BufferedReader(reader);
    System.out.println("What do you want the program to do?");
    System.out.println(" Decimal to binary ... 1");
    System.out.println(" Binary to decimal ... 2");
    String choice = input.readLine();
    float c = Integer.parseInt(choice);

    if(c==1)
    {
       System.out.println(" Enter a decimal number:");
       String dba=input.readLine();
       int db = Integer.parseInt(dba);
       System.out.println(db);
       int [] m = new int [10];
       int j;
        for(j=8;j>=1;j--)
           {
                  m[j] = db%2;
              db = db/2;
           }
                float sc = 0;
                  for(j=1;j<=8;j++)
            {
               if(m[j]==0)
                 {
                               if(sc==0)
                                    {
                                       continue;
                                    }
                 }
                        System.out.print(+m[j] + " ");
                        sc = sc+1;
                        }
    }

   if(c==2)
   {
      System.out.println("Enter a binary number");
      String bda=input.readLine();
      float l=bda.length()-1;
      double sum =0;
       for(int w =0;w<bda.length();w++)
       {
              char r = bda.charAt(w);
              String str = new Character®.toString();
          float er = Integer.parseInt(str);
          sum = sum + (er*Math.pow(2,l));
              l--;
          }
        String zero = "";
        for (int i = 0; i < 16 - bda.length(); i++)
        {
            zero = zero + "0";
        }
        bda = bda + zero;
        
       System.out.println("The decimal is : "+sum);
   }

  
}
    private static String Zeroes(String bda)
    {
        String zero = "";
        for (int i = 0; i < 16 - bda.length(); i++)
        {
            zero = zero + "0";
        }
        bda = bda + zero;
        return bda;
    }
}





I have to input 16 bit number 0000010101100000 as a input, and a program have to take first 8 bit numbers 00000101 and convert it to decimal(answer is 5) and after that have to put a point(.) and select next 8 bits 01100000 and this number should work as floating point and it should give you 0.375
So the answer of my program is 5.375
That what my program have to do.


My instructor told us to Write a conversion program for a decimal number to binary and vice versa. Binary numbers are of 16 bits: 8 bits for the whole number and 8 bits for the decimal part.


Please I need your help to creat a function that can do what I wrote above.

Please help to creat it.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:47PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month