6 Replies - 1646 Views - Last Post: 29 September 2007 - 10:39 AM Rate Topic: -----

#1 jack85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-September 07

need help with my binary numbers

Post icon  Posted 28 September 2007 - 09:29 AM

A GUYS I AM TRYING TO CREATE A JAVA PROGRAM THAT TAKES BINARY NUMBER AND TRANSFER TO NEGATIVE DECIMAL NUMBER. AND ALSO TAKES NEGATIVE DECIMAL NUMBERS AND TRANSFER TO BINARY NUMBERS. MY PROGRAM HAVE TO BE 8-BITS.
I AM TRYING TO CREAT MY PROGRAM, BUT IT GIVES ME A LOT OF ERRORS THAT I CAN FIX IT. PLEASE SOMEONE CAN HELP ME TO CREAT THIS PROGRAM. THANKS GUYS.

EX: BINARY->DECIMAL
INPUT: 111111001
OUTPUT: -7

EX: DECIMAL->BINARY
INPUT:-7
OUTPUT:111111001

MUCH DETAILS:

HERES HOW IT IS WORKING: BINARY->DECIMAL
YOU PUT 1001 AND IT FLIPS TO 0110. THEN YOU USE ALGORITME FOR 0110 AND IT GIVES YOU 6. THEN YOU ADD ONE AND IT GIVES YOU 7. AFTER THAT YOU INPUT NEGATIVE IN FRONT, AND YOUR FINAL RESULT BECOMES -7.

DECIMAL->BINARY:
YOU PUT 7 AND THEN ADD -1 GIVES YOU 6. THEN YOU USE ALGORITM YOU GET 6->0110. AFTER THAT FLIT THE NUMBER 0110->1001 AND IT WILL BE YOU ANSWER.

Is This A Good Question/Topic? 0
  • +

Replies To: need help with my binary numbers

#2 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: need help with my binary numbers

Posted 28 September 2007 - 10:39 AM

What you are describing is simple conversion between decimal and two's complement. From your description, it sounds like you understand how it works. Nowhere in your post do you actually list your problems. We can't help you unless you tell us what problems you are having.

Also, typing in all caps is annoying. It's the equivalent of yelling. Best to avoid it.

This post has been edited by Programmist: 28 September 2007 - 10:41 AM

Was This Post Helpful? 0
  • +
  • -

#3 jack85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-September 07

Re: need help with my binary numbers

Posted 28 September 2007 - 10:46 AM

View PostProgrammist, on 28 Sep, 2007 - 10:39 AM, said:

What you are describing is simple conversion between decimal and two's complement. From your description, it sounds like you understand how it works. Nowhere in your post do you actually list your problems. We can't help you unless you tell us what problems you are having.

Also, typing in all caps is annoying. It's the equivalent of yelling. Best to avoid it.



my problem is that I can't replace i with 0 or with 1. Its says that int to int is not posible to replace.


package Bynary;
import java.util.*;
import java.io.*;

public class Program
{
		
	   
	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();
		int 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;
			}
			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;
			}
			input.readLine();
		}

		if(c==2)
		{
			System.out.println("Enter a binary number");
			String bda=input.readLine();
			int l=bda.length()-1;
			double sum =0;
			for (int i = bda.length() - 1; i >= 0; i--)
			{
				int j = bda.indexOf(i);

				if (i <= 0)
				{
											
					bda = bda.replace(i, 0);
					j = bda.indexOf(i);
					//bda = bda + "1";
				}
					
				else if (i >= 1)
				{
					bda = bda.replace(i, 1);
					//bda = bda + "1";
				}
							   
			}

						
			for(int w =0;w<bda.length();w++)
			{
				char r = bda.charAt(w);
				String str = new Character®.toString();
				int er = Integer.parseInt(str);
				sum = sum + (er*Math.pow(2,l));
				l--;
			}
						   
			System.out.println("The decimal is : "+sum);
			System.out.println("-" + sum);
			input.readLine();
						   
		}

	}
}
[edit] added code tags

This post has been edited by PennyBoki: 28 September 2007 - 11:28 AM

Was This Post Helpful? 0
  • +
  • -

#4 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: need help with my binary numbers

Posted 28 September 2007 - 11:15 AM

Look at the String.replace API. It takes chars and you are passing ints.

Also, when you post code, put it in code tags. Another thing. It's usually best to copy and paste your exact error message. Thanks.
Was This Post Helpful? 0
  • +
  • -

#5 jack85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-September 07

Re: need help with my binary numbers

Posted 28 September 2007 - 11:51 AM

A GUYS I AM TRYING TO CREATE A JAVA PROGRAM THAT TAKES BINARY NUMBER AND TRANSFER TO NEGATIVE DECIMAL NUMBER. AND ALSO TAKES NEGATIVE DECIMAL NUMBERS AND TRANSFER TO BINARY NUMBERS. MY PROGRAM HAVE TO BE 8-BITS.
I AM TRYING TO CREAT MY PROGRAM, BUT IT GIVES ME A LOT OF ERRORS THAT I CAN FIX IT. PLEASE SOMEONE CAN HELP ME TO CREAT THIS PROGRAM. THANKS GUYS.

EX: BINARY->DECIMAL
INPUT: 111111001
OUTPUT: -7

EX: DECIMAL->BINARY
INPUT:-7
OUTPUT:111111001

MUCH DETAILS:

HERES HOW IT IS WORKING: BINARY->DECIMAL
YOU PUT 1001 AND IT FLIPS TO 0110. THEN YOU USE ALGORITME FOR 0110 AND IT GIVES YOU 6. THEN YOU ADD ONE AND IT GIVES YOU 7. AFTER THAT YOU INPUT NEGATIVE IN FRONT, AND YOUR FINAL RESULT BECOMES -7.

DECIMAL->BINARY:
YOU PUT 7 AND THEN ADD -1 GIVES YOU 6. THEN YOU USE ALGORITM YOU GET 6->0110. AFTER THAT FLIT THE NUMBER 0110->1001 AND IT WILL BE YOU ANSWER.


my problem is that I can't replace i with 0 or with 1. Its says that int to int is not posible to replace.


package Bynary;
import java.util.*;
import java.io.*;

public class Program
{
		
	   
	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();
		int 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;
			}
			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;
			}
			input.readLine();
		}

		if(c==2)
		{
			System.out.println("Enter a binary number");
			String bda=input.readLine();
			int l=bda.length()-1;
			double sum =0;
			for (int i = bda.length() - 1; i >= 0; i--)
			{
				int j = bda.indexOf(i);

				if (i <= 0)
				{
											
					bda = bda.replace(i, 0);
					j = bda.indexOf(i);
					//bda = bda + "1";
				}
					
				else if (i >= 1)
				{
					bda = bda.replace(i, 1);
					//bda = bda + "1";
				}
							   
			}

						
			for(int w =0;w<bda.length();w++)
			{
				char r = bda.charAt(w);
				String str = new Character®.toString();
				int er = Integer.parseInt(str);
				sum = sum + (er*Math.pow(2,l));
				l--;
			}
						   
			System.out.println("The decimal is : "+sum);
			System.out.println("-" + sum);
			input.readLine();
						   
		}

	}
}

This post has been edited by PennyBoki: 28 September 2007 - 12:48 PM

Was This Post Helpful? 0
  • +
  • -

#6 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: need help with my binary numbers

Posted 28 September 2007 - 12:48 PM

topics merged, there is no need for a new thread, and please use code tags. for the code
Was This Post Helpful? 0
  • +
  • -

#7 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: need help with my binary numbers

Posted 29 September 2007 - 10:39 AM

What - you don't like my answer, so you re-post the same question again? Read my post. It has the answer. I'm not going to spoon feed it to you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1