# binary

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-02-2010, 02:01 PM
Engineer
binary
Hello:)
how can i convert from decimal to binary in n-bits
that user can enter or limit n
example
the number is 3
n=4
by binary=0011......
???
thanks
• 12-02-2010, 02:05 PM
Engineer
My attempts
Code:

```System.out.println("Enter your number");     number=sc.nextInt();     String bin=Integer.toBinaryString(number);     System.out.println(bin);```
but i cant compelet:o
• 12-02-2010, 03:02 PM
Vinod Mukundan
Quote:

Originally Posted by Engineer
My attempts
Code:

```System.out.println("Enter your number");     number=sc.nextInt();     String bin=Integer.toBinaryString(number);     System.out.println(bin);```
but i cant compelet:o

Well make clear what you want ..... By the way I just modified your code a bit ..... see if it is useful ...

Code:

```      System.out.println("Enter your number");       int number=sc.nextInt();       System.out.println("Enter your limit");       int limit=sc.nextInt();       String bin=Integer.toBinaryString(number);       if(bin.length()>limit){           bin =  bin.substring(0, limit);       }       System.out.println(bin);```
:p

Happy Coding ...

warm regards
Vinod M
• 12-02-2010, 03:21 PM
Engineer
Quote:

Originally Posted by Vinod Mukundan
Well make clear what you want ..... By the way I just modified your code a bit ..... see if it is useful ...

Code:

```      System.out.println("Enter your number");       int number=sc.nextInt();       System.out.println("Enter your limit");       int limit=sc.nextInt();       String bin=Integer.toBinaryString(number);       if(bin.length()>limit){           bin =  bin.substring(0, limit);       }       System.out.println(bin);```
:p

Happy Coding ...

warm regards
Vinod M

thank you for help:)
i use your code this is result:
3
4
11
i dont mean that
i mean that it is
0011 not 11:o
i dont understand
Code:

`bin =  bin.substring(0, limit);`
thanks
• 12-02-2010, 03:24 PM
Vinod Mukundan
3
6

ok so wat will this should give you ???

Integer.toBinaryString(3); will give you 11 .... so wat u want is if the limit is 4 then we should add two 0's in front ??? is that it or ?
• 12-02-2010, 03:27 PM
Engineer
Quote:

Originally Posted by Vinod Mukundan
3
6

ok so wat will this should give you ???

it give me
000011
• 12-02-2010, 03:30 PM
Engineer
Quote:

Originally Posted by Vinod Mukundan
3
6

ok so wat will this should give you ???

Integer.toBinaryString(3); will give you 11 .... so wat u want is if the limit is 4 then we should add two 0's in front ??? is that it or ?

yes, iwant to make the number by limit
Ex:
limit=8
the number=5
binary=00000101
• 12-02-2010, 03:33 PM
Vinod Mukundan
Code:

``` System.out.println("Enter your number");         int number = sc.nextInt();         System.out.println("Enter your limit");         int limit = sc.nextInt();         String bin = Integer.toBinaryString(number);         if (bin.length() < limit) {             while (bin.length() != limit) {                 bin = "0" + bin;             }         }         System.out.println(bin);     }```

try this
• 12-02-2010, 03:37 PM
Engineer
Quote:

Originally Posted by Vinod Mukundan
Code:

``` System.out.println("Enter your number");         int number = sc.nextInt();         System.out.println("Enter your limit");         int limit = sc.nextInt();         String bin = Integer.toBinaryString(number);         if (bin.length() < limit) {             while (bin.length() != limit) {                 bin = "0" + bin;             }         }         System.out.println(bin);     }```

try this

yes:)
thanks alot thats what i want:):)
• 12-02-2010, 03:41 PM
Vinod Mukundan
Code:

```System.out.println("Enter your number");         int number = sc.nextInt();         System.out.println("Enter your limit");         int limit = sc.nextInt();         StringBuilder sb = new StringBuilder();         String bin = Integer.toBinaryString(number);         for (int i = bin.length(); i < limit; i++) {                 sb.append("0");         }         sb.append(bin);         System.out.println(sb.toString());     }```

Well use this one ... as String is immutable its better to use the builder instead...

ofcourse if you give 5 and limit 2 it will still print 101 ... do u need to overcome that also ?? try it

Happy Coding ..

warm regards
Vinod M
• 12-02-2010, 04:12 PM
Engineer
Quote:

Originally Posted by Vinod Mukundan
Code:

```System.out.println("Enter your number");         int number = sc.nextInt();         System.out.println("Enter your limit");         int limit = sc.nextInt();         StringBuilder sb = new StringBuilder();         String bin = Integer.toBinaryString(number);         for (int i = bin.length(); i < limit; i++) {                 sb.append("0");         }         sb.append(bin);         System.out.println(sb.toString());     }```
Well use this one ... as String is immutable its better to use the builder instead...

ofcourse if you give 5 and limit 2 it will still print 101 ... do u need to overcome that also ?? try it

Happy Coding ..

warm regards
Vinod M

thanks.................:)
i have aquestion
if i want to add to binary number
and the result in limit bits as number
how can i delete the carry end?????????????
and know that if its 1or0?????
• 12-03-2010, 06:14 AM
Vinod Mukundan
Quote:

Originally Posted by Engineer
thanks.................:)
i have aquestion
if i want to add to binary number
and the result in limit bits as number
how can i delete the carry end?????????????
and know that if its 1or0?????

Can you give an example for the same ....... coz am a very lazy guy ... :p
• 12-03-2010, 08:16 AM
Engineer
Quote:

Originally Posted by Vinod Mukundan
Can you give an example for the same ....... coz am a very lazy guy ... :p

hhhhhhhhhhhhhh:)
ok,
if i have
0011
+
1011
----------
10110
i need to know if the first element (0) or (1), in this example(0)
but in the program how can i know that???????????
in the program if the first element==0...........yes
else no??????????
and how i can delete the last element(1)???

my attempets
put the number in array to know addressesfor the first number or last
but failed:o:o
• 12-03-2010, 08:24 AM
Vinod Mukundan
Quote:

i need to know if the first element (0) or (1), in this example(0)
but in the program how can i know that???????????
Try
Code:

```            if(bin.endsWith("0")) {             }else{           }```
By the way u r reading string in ----> or <------ direction coz as far as i look the first element is 1 in the output 10110
• 12-03-2010, 08:25 AM
Vinod Mukundan
Quote:

and how i can delete the last element(1)???
you mean just the last element or delete to a particular length ???
• 12-03-2010, 08:31 AM
Engineer
last element
• 12-03-2010, 08:36 AM
Vinod Mukundan
Quote:

Originally Posted by Engineer
last element

ok as far as I guess u need to limit the output .Check the below and lemme knw if u need anymore clarification

Code:

```           if(bin.endsWith("0")) {                     //Do watever you want to do if its 0           }else{           }           if(bin.length()>limit) {           bin = bin.substring(bin.length()-limit, bin.length());         }```
IF you just want the first element replace limit with 1 :) an yeah take out the if condition
• 12-03-2010, 09:31 AM
Engineer
thanks alot .............
how i know the last number i delet?????????
and print it alone???????????
• 12-03-2010, 09:52 AM
JosAH
Quote:

Originally Posted by Engineer
0011
+
1011
----------
10110

That is a severely broken ALU.

kind regards,

Jos
• 12-03-2010, 10:06 AM
Vinod Mukundan
Quote:

Originally Posted by Engineer
thanks alot .............
how i know the last number i delet?????????
and print it alone???????????

By last number you mean 1 out of 1010 ??? then jus try

bin.charAt(0);
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last