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

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




need help

 
Reply to this topicStart new topic

need help, taking first bits of a binary number

jack85
17 Oct, 2007 - 12:02 PM
Post #1

New D.I.C Head
*

Joined: 28 Sep, 2007
Posts: 34


My Contributions
cool.gif if I input 16 bits number in my binary, how can I tak e only first 8 of them.
ex: 1001010100100010
I only have to take 10010101 first 8 of them. Can someone help me with this . I need a function that can do this stuff.


this is 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, A;
           for(j=8;j>=1;j--)
           {
                  m[j] = db%2;
              db = db/2;
           }
                int 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--;
              
          }
              
       System.out.println("The decimal is : "+sum);
   }

  
}
    
}

User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: Need Help
17 Oct, 2007 - 12:16 PM
Post #2

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 15,262



Thanked: 61 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
Please put your code in between [ code ] tags like the instructions say.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Need Help
17 Oct, 2007 - 12:45 PM
Post #3

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
before converting to a number, simply use the String.substring function, or if it is already a number, divide it by 2^8 which is the same thing as using the >>operator and shifting it 8 bits to the left.
and please don't post the same question in multiple threads, thank you.
User is offlineProfile CardPM
+Quote Post

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

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