need help with my binary numbers

Page 1 of 1

6 Replies - 2027 Views - Last Post: 29 September 2007 - 10:39 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=34191&amp;s=3bb4bca09586a690b7a0e5c99edec7b1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 jack85

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

need help with my binary numbers

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

• Refactorer in Chief

Reputation: 255
• Posts: 1,843
• 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

#3 jack85

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

Re: need help with my binary numbers

Posted 28 September 2007 - 10:46 AM

Programmist, 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
{
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");
int c = Integer.parseInt(choice);

if(c==1)
{

System.out.println(" Enter a decimal number:");

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;
}
}

if(c==2)
{
System.out.println("Enter a binary number");
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);

}

}
}
```

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

#4 Programmist

• Refactorer in Chief

Reputation: 255
• Posts: 1,843
• 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.

#5 jack85

Reputation: 0
• 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
{
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");
int c = Integer.parseInt(choice);

if(c==1)
{

System.out.println(" Enter a decimal number:");

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;
}
}

if(c==2)
{
System.out.println("Enter a binary number");
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);

}

}
}
```

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

#6 PennyBoki

• D.I.C Lover

Reputation: 55
• Posts: 2,345
• 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

#7 Programmist

• Refactorer in Chief

Reputation: 255
• Posts: 1,843
• 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.