Results 1 to 20 of 23
Thread: binary
- 12-02-2010, 01:01 PM #1
Member
- Join Date
- Aug 2010
- Location
- Egypt
- Posts
- 34
- Rep Power
- 0
- 12-02-2010, 01:05 PM #2
Member
- Join Date
- Aug 2010
- Location
- Egypt
- Posts
- 34
- Rep Power
- 0
My attempts
but i cant compelet:oPHP Code:System.out.println("Enter your number"); number=sc.nextInt(); String bin=Integer.toBinaryString(number); System.out.println(bin);
- 12-02-2010, 02:02 PM #3
Well make clear what you want ..... By the way I just modified your code a bit ..... see if it is useful ...
:pJava 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);
Happy Coding ...
warm regards
Vinod M_______________________________________________
give me beans .........
- 12-02-2010, 02:21 PM #4
Member
- Join Date
- Aug 2010
- Location
- Egypt
- Posts
- 34
- Rep Power
- 0
- 12-02-2010, 02:24 PM #5
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 ?Last edited by Vinod Mukundan; 12-02-2010 at 02:27 PM.
_______________________________________________
give me beans .........
- 12-02-2010, 02:27 PM #6
Member
- Join Date
- Aug 2010
- Location
- Egypt
- Posts
- 34
- Rep Power
- 0
- 12-02-2010, 02:30 PM #7
Member
- Join Date
- Aug 2010
- Location
- Egypt
- Posts
- 34
- Rep Power
- 0
- 12-02-2010, 02:33 PM #8
Java 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_______________________________________________
give me beans .........
- 12-02-2010, 02:37 PM #9
Member
- Join Date
- Aug 2010
- Location
- Egypt
- Posts
- 34
- Rep Power
- 0
- 12-02-2010, 02:41 PM #10
Java 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_______________________________________________
give me beans .........
- 12-02-2010, 03:12 PM #11
Member
- Join Date
- Aug 2010
- Location
- Egypt
- Posts
- 34
- Rep Power
- 0
- 12-03-2010, 05:14 AM #12
- 12-03-2010, 07:16 AM #13
Member
- Join Date
- Aug 2010
- Location
- Egypt
- Posts
- 34
- Rep Power
- 0
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
- 12-03-2010, 07:24 AM #14
Tryi 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 10110Java Code:if(bin.endsWith("0")) { }else{ }_______________________________________________
give me beans .........
- 12-03-2010, 07:25 AM #15
you mean just the last element or delete to a particular length ???and how i can delete the last element(1)???_______________________________________________
give me beans .........
- 12-03-2010, 07:31 AM #16
Member
- Join Date
- Aug 2010
- Location
- Egypt
- Posts
- 34
- Rep Power
- 0
last element
- 12-03-2010, 07:36 AM #17
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 conditionJava 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()); }Last edited by Vinod Mukundan; 12-03-2010 at 07:38 AM.
_______________________________________________
give me beans .........
- 12-03-2010, 08:31 AM #18
Member
- Join Date
- Aug 2010
- Location
- Egypt
- Posts
- 34
- Rep Power
- 0
thanks alot .............
how i know the last number i delet?????????
and print it alone???????????
- 12-03-2010, 08:52 AM #19
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 12-03-2010, 09:06 AM #20
Similar Threads
-
Binary
By THEAniKan in forum EntertainmentReplies: 1Last Post: 01-06-2012, 10:14 AM -
Binary IO help
By kosok in forum New To JavaReplies: 2Last Post: 12-25-2008, 07:58 AM -
get binary value
By rushenas in forum New To JavaReplies: 2Last Post: 06-14-2008, 01:51 PM -
binary conversion
By thamizhisai in forum New To JavaReplies: 3Last Post: 04-29-2008, 11:55 AM -
Binary Addition
By Deo Favente in forum Advanced JavaReplies: 11Last Post: 04-24-2008, 05:34 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks