Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: binary

  1. #1
    Engineer is offline Member
    Join Date
    Aug 2010
    Location
    Egypt
    Posts
    34
    Rep Power
    0

    Default 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

  2. #2
    Engineer is offline Member
    Join Date
    Aug 2010
    Location
    Egypt
    Posts
    34
    Rep Power
    0

    Default

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

  3. #3
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Quote Originally Posted by Engineer View Post
    My attempts
    PHP 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 ...

    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){
               bin =  bin.substring(0, limit);
           }
          System.out.println(bin);
    :p

    Happy Coding ...

    warm regards
    Vinod M
    _______________________________________________
    give me beans .........

  4. #4
    Engineer is offline Member
    Join Date
    Aug 2010
    Location
    Egypt
    Posts
    34
    Rep Power
    0

    Default

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

    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){
               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:
    Enter your number
    3
    Enter your limit
    4
    11
    i dont mean that
    i mean that it is
    0011 not 11:o
    i dont understand
    PHP Code:
    bin =  bin.substring(0, limit);
    thanks

  5. #5
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    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 .........

  6. #6
    Engineer is offline Member
    Join Date
    Aug 2010
    Location
    Egypt
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Vinod Mukundan View Post
    Enter your number
    3
    Enter your limit
    6

    ok so wat will this should give you ???
    it give me
    000011

  7. #7
    Engineer is offline Member
    Join Date
    Aug 2010
    Location
    Egypt
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Vinod Mukundan View Post
    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 ?
    yes, iwant to make the number by limit
    Ex:
    limit=8
    the number=5
    binary=00000101

  8. #8
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    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 .........

  9. #9
    Engineer is offline Member
    Join Date
    Aug 2010
    Location
    Egypt
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Vinod Mukundan View Post
    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
    yes:)
    thanks alot thats what i want:):)

  10. #10
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    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 .........

  11. #11
    Engineer is offline Member
    Join Date
    Aug 2010
    Location
    Egypt
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Vinod Mukundan View Post
    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
    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. #12
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Quote Originally Posted by Engineer View Post
    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
    _______________________________________________
    give me beans .........

  13. #13
    Engineer is offline Member
    Join Date
    Aug 2010
    Location
    Egypt
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Vinod Mukundan View Post
    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 failedo

  14. #14
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    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
    Java 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
    _______________________________________________
    give me beans .........

  15. #15
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    and how i can delete the last element(1)???
    you mean just the last element or delete to a particular length ???
    _______________________________________________
    give me beans .........

  16. #16
    Engineer is offline Member
    Join Date
    Aug 2010
    Location
    Egypt
    Posts
    34
    Rep Power
    0

    Default

    last element

  17. #17
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Quote Originally Posted by Engineer View Post
    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

    Java 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
    Last edited by Vinod Mukundan; 12-03-2010 at 07:38 AM.
    _______________________________________________
    give me beans .........

  18. #18
    Engineer is offline Member
    Join Date
    Aug 2010
    Location
    Egypt
    Posts
    34
    Rep Power
    0

    Default

    thanks alot .............
    how i know the last number i delet?????????
    and print it alone???????????

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Engineer View Post
    0011
    +
    1011
    ----------
    10110
    That is a severely broken ALU.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Quote Originally Posted by Engineer View Post
    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);
    _______________________________________________
    give me beans .........

Page 1 of 2 12 LastLast

Similar Threads

  1. Binary
    By THEAniKan in forum Entertainment
    Replies: 1
    Last Post: 01-06-2012, 10:14 AM
  2. Binary IO help
    By kosok in forum New To Java
    Replies: 2
    Last Post: 12-25-2008, 07:58 AM
  3. get binary value
    By rushenas in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 01:51 PM
  4. binary conversion
    By thamizhisai in forum New To Java
    Replies: 3
    Last Post: 04-29-2008, 11:55 AM
  5. Binary Addition
    By Deo Favente in forum Advanced Java
    Replies: 11
    Last Post: 04-24-2008, 05:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •