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
Printable View
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
My attempts
but i cant compelet:oCode:System.out.println("Enter your number");
number=sc.nextInt();
String bin=Integer.toBinaryString(number);
System.out.println(bin);
Well make clear what you want ..... By the way I just modified your code a bit ..... see if it is useful ...
:pCode: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);
Happy Coding ...
warm regards
Vinod M
Enter your number
3
Enter your limit
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 ?
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
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
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
TryQuote:
i need to know if the first element (0) or (1), in this example(0)
but in the program how can i know that???????????
By the way u r reading string in ----> or <------ direction coz as far as i look the first element is 1 in the output 10110Code:if(bin.endsWith("0")) {
}else{
}
you mean just the last element or delete to a particular length ???Quote:
and how i can delete the last element(1)???
last element
and wat about the direction i asked in previous post :p
ok as far as I guess u need to limit the output .Check the below and lemme knw if u need anymore clarification
IF you just want the first element replace limit with 1 :) an yeah take out the if conditionCode:
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());
}
thanks alot .............
how i know the last number i delet?????????
and print it alone???????????