# binary number progblem

Page 1 of 1

## 10 Replies - 1834 Views - Last Post: 02 October 2007 - 07:08 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=34325&amp;s=d4c6674e355a0f006e9ae9d262d9c735&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

# binary number progblem

Posted 30 September 2007 - 10:39 AM

A guys I have a problem when I am trying to flip the numbers between 0 and 1. It's in my switch loop. It doesn't do it at all.

Ex: 1001->0110.
```package Bynary;
import java.util.*;
import java.io.*;

public class Bynary
{

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 = 0; i <= bda.length(); i++)
{
switch(i)
{
case '0': bda =  bda + "0";
break;
case '1': bda = bda + "1";
break;
default:
System.out.println("Thanks");
}
}
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("The decimal is : "+sum);
System.out.println("-" + sum);

}
}
}
```

Is This A Good Question/Topic? 0

## Replies To: binary number progblem

### #2 PennyBoki

• D.I.C Lover

Reputation: 53
• Posts: 2,335
• Joined: 11-December 06

## Re: binary number progblem

Posted 30 September 2007 - 10:50 AM

OK first this code is for bynary to decimal and vise versa(sort of)

My suggestion to do 1001->0110 is, this little binary arithmetic thing:
1111-1001=0110

### #3 jack85

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

## Re: binary number progblem

Posted 30 September 2007 - 10:54 AM

PennyBoki, on 30 Sep, 2007 - 10:50 AM, said:

OK first this code is for bynary to decimal and vise versa(sort of)

My suggestion to do 1001->0110 is, this little binary arithmetic thing:
1111-1001=0110

The point is it can be any number. I just use one of the them, to show the example. Please you have another idea how can i fix my program.

### #4 PennyBoki

• D.I.C Lover

Reputation: 53
• Posts: 2,335
• Joined: 11-December 06

## Re: binary number progblem

Posted 30 September 2007 - 11:04 AM

jack85, on 30 Sep, 2007 - 10:54 AM, said:

PennyBoki, on 30 Sep, 2007 - 10:50 AM, said:

OK first this code is for bynary to decimal and vise versa(sort of)

My suggestion to do 1001->0110 is, this little binary arithmetic thing:
1111-1001=0110

The point is it can be any number. I just use one of the them, to show the example. Please you have another idea how can i fix my program.

I know that. I give also an example based on your example. So you need to know the number of bits of the number that the user enters. Another way to do this is with string manipulation, take the input of the user as a string then just replace the 0s with 1s and the 1s with 0s.

### #5 jack85

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

## Re: binary number progblem

Posted 30 September 2007 - 11:09 AM

PennyBoki, on 30 Sep, 2007 - 11:04 AM, said:

jack85, on 30 Sep, 2007 - 10:54 AM, said:

PennyBoki, on 30 Sep, 2007 - 10:50 AM, said:

OK first this code is for bynary to decimal and vise versa(sort of)

My suggestion to do 1001->0110 is, this little binary arithmetic thing:
1111-1001=0110

The point is it can be any number. I just use one of the them, to show the example. Please you have another idea how can i fix my program.

I know that. I give also an example based on your example. So you need to know the number of bits of the number that the user enters. Another way to do this is with string manipulation, take the input of the user as a string then just replace the 0s with 1s and the 1s with 0s.

That what I am trying to do with my swtich loop, but it doesn't working. If ti si posible can you show me a example that how can I fix my switch loop that it can work.

### #6 PennyBoki

• D.I.C Lover

Reputation: 53
• Posts: 2,335
• Joined: 11-December 06

## Re: binary number progblem

Posted 30 September 2007 - 11:44 AM

ok here is an example:

```public class Example
{
public static void main(String [] args)
{
String aaa="0100010101";//some input string

char[] array;// some array of chars

String tmp = new String();//tmp = temporary string
tmp=aaa;//give tha value of aaa to tmp
array=tmp.toCharArray();// fill the array with the chars of the tmp string
for(int i=0; i<aaa.length();i++)// loop through the array
{
if(array[i]=='1')//if the char at that poistion is 1
{
array[i]='0';	//make it 0
}
else array[i]='1';	//if not make it 1

}//end the for loop
aaa = new String(array);//make the array string

System.out.println(aaa);//print the complement of the new string
}
}
```

### #7 jack85

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

## Re: binary number progblem

Posted 30 September 2007 - 12:03 PM

PennyBoki, on 30 Sep, 2007 - 11:44 AM, said:

ok here is an example:

```public class Example
{
public static void main(String [] args)
{
String aaa="0100010101";//some input string

char[] array;// some array of chars

String tmp = new String();//tmp = temporary string
tmp=aaa;//give tha value of aaa to tmp
array=tmp.toCharArray();// fill the array with the chars of the tmp string
for(int i=0; i<aaa.length();i++)// loop through the array
{
if(array[i]=='1')//if the char at that poistion is 1
{
array[i]='0';	//make it 0
}
else array[i]='1';	//if not make it 1

}//end the for loop
aaa = new String(array);//make the array string

System.out.println(aaa);//print the complement of the new string
}
}
```

Thanks it worked.

### #8 jack85

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

## Re: binary number progblem

Posted 30 September 2007 - 01:20 PM

jack85, on 30 Sep, 2007 - 12:03 PM, said:

PennyBoki, on 30 Sep, 2007 - 11:44 AM, said:

ok here is an example:

```public class Example
{
public static void main(String [] args)
{
String aaa="0100010101";//some input string

char[] array;// some array of chars

String tmp = new String();//tmp = temporary string
tmp=aaa;//give tha value of aaa to tmp
array=tmp.toCharArray();// fill the array with the chars of the tmp string
for(int i=0; i<aaa.length();i++)// loop through the array
{
if(array[i]=='1')//if the char at that poistion is 1
{
array[i]='0';	//make it 0
}
else array[i]='1';	//if not make it 1

}//end the for loop
aaa = new String(array);//make the array string

System.out.println(aaa);//print the complement of the new string
}
}
```

Thanks it worked.

A bro I have one more problem also. In my Decimal -> Binary. I can't reverse a number. It only work in
Binary -> Decimal function. It didn't revers at all.

### #9 jack85

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

## Re: binary number progblem

Posted 01 October 2007 - 07:54 AM

jack85, on 30 Sep, 2007 - 01:20 PM, said:

jack85, on 30 Sep, 2007 - 12:03 PM, said:

PennyBoki, on 30 Sep, 2007 - 11:44 AM, said:

ok here is an example:

```public class Example
{
public static void main(String [] args)
{
String aaa="0100010101";//some input string

char[] array;// some array of chars

String tmp = new String();//tmp = temporary string
tmp=aaa;//give tha value of aaa to tmp
array=tmp.toCharArray();// fill the array with the chars of the tmp string
for(int i=0; i<aaa.length();i++)// loop through the array
{
if(array[i]=='1')//if the char at that poistion is 1
{
array[i]='0';	//make it 0
}
else array[i]='1';	//if not make it 1

}//end the for loop
aaa = new String(array);//make the array string

System.out.println(aaa);//print the complement of the new string
}
}
```

Thanks it worked.

A bro I have one more problem also. In my Decimal -> Binary. I can't reverse a number. It only work in
Binary -> Decimal function. It didn't revers at all.

A guys can someone help me with my problem.

A bro I have one more problem also. In my Decimal -> Binary. I can't reverse a number. It only work in
Binary -> Decimal function. It didn't revers at all.

### #10 jack85

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

## Re: binary number progblem

Posted 01 October 2007 - 06:28 PM

jack85, on 1 Oct, 2007 - 07:54 AM, said:

jack85, on 30 Sep, 2007 - 01:20 PM, said:

jack85, on 30 Sep, 2007 - 12:03 PM, said:

PennyBoki, on 30 Sep, 2007 - 11:44 AM, said:

ok here is an example:

```public class Example
{
public static void main(String [] args)
{
String aaa="0100010101";//some input string

char[] array;// some array of chars

String tmp = new String();//tmp = temporary string
tmp=aaa;//give tha value of aaa to tmp
array=tmp.toCharArray();// fill the array with the chars of the tmp string
for(int i=0; i<aaa.length();i++)// loop through the array
{
if(array[i]=='1')//if the char at that poistion is 1
{
array[i]='0';	//make it 0
}
else array[i]='1';	//if not make it 1

}//end the for loop
aaa = new String(array);//make the array string

System.out.println(aaa);//print the complement of the new string
}
}
```

Thanks it worked.

A bro I have one more problem also. In my Decimal -> Binary. I can't reverse a number. It only work in
Binary -> Decimal function. It didn't revers at all.

A guys can someone help me with my problem.

A bro I have one more problem also. In my Decimal -> Binary. I can't reverse a number. It only work in
Binary -> Decimal function. It didn't revers at all.

Need Help

### #11 PennyBoki

• D.I.C Lover

Reputation: 53
• Posts: 2,335
• Joined: 11-December 06

## Re: binary number progblem

Posted 02 October 2007 - 07:08 AM

You have to be patient, this is a forum not live chat, that means you may get answer sooner or later or maybe you wont get one at all. Instead of the time waiting for someone to reply, try solving this yourself.