Results 1 to 10 of 10

Thread: binary _ array

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

    Default binary _ array

    Hello,:)
    if i have binary number
    ex
    0011
    how can i make this number in array
    [0]=1
    [1]=1
    [2]=0
    [3]=0
    ????????

  2. #2
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

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

    Default

    Quote Originally Posted by tashimoto View Post
    thanks:)
    i can make an array, but the problem in how can make the binary number in array ???????????????????????

  4. #4
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Well, this depends. How are you getting the binary number? Are you going to be passed 1's and 0's via a string. Are you going to be passed an integer? Can the integer be signed or unsigned?

    I can't give you an unambiguous response if you give me an ambiguous question.

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

    Default

    Quote Originally Posted by Mr.Beans View Post
    Well, this depends. How are you getting the binary number? Are you going to be passed 1's and 0's via a string. Are you going to be passed an integer? Can the integer be signed or unsigned?

    I can't give you an unambiguous response if you give me an ambiguous question.
    ok
    PHP Code:
      try{
         System.out.println("Enter your multiplier");
         multiplier=sc.nextInt();
         
         StringBuilder sb = new StringBuilder();
            bin = Integer.toBinaryString(multiplier);
            for (int i = bin.length(); i < limit; i++) {
                    sb.append("0");
            }
            sb.append(bin);
            System.out.println(sb.toString());
           
    
            }
         catch(Exception e){
    
           
            System.out.println("Enter number *_^");
    
    
            }
    this my code i want the program to take the binary number and but every element in array
    Ex:
    0101
    i want to take the first element(1) .............
    to take it we should put it in array to make address for it as [0]=1, right??

  6. #6
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    That did not help much. However, I see that you are using the toBinaryString method from Integer. Since you have a string of all the binary digits, you simply have to split them up and put them in the array.
    This also brings about the question of whether you want an array of integers, (0 or 1) or characters ('0' or '1').

    Either way, you can get each individual character in the string using the charAt(int index) method. You can create a for loop which goes through and gets each character in the string, then you can handle each character how you wish.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Use a for loop. The StringBuilder method charAt() will give you the character at each position and all you need to do is look at it and decide whether to put a 0 or a 1 into the array.

    Note that int arrays are initialised with zeros in them so you might be able to avoid padding the bin string with zero digits.

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

    Default

    sorry,but i cant understand
    please,can any one put acode to right understand
    thanks

  9. #9
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Java Code:
    String s = Integer.toBinaryString(x);
    
    for(int i = 0; i < s.length(); i++) {
        char digit = s.charAt(i);
        // Do what you want with digit here
    }
    That is the basics of what you want to do. You have to find out the specifics for yourself, as it is your problem.

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

    Default

    Quote Originally Posted by Engineer View Post
    Hello,:)
    if i have binary number
    ex
    0011
    how can i make this number in array
    [0]=1
    [1]=1
    [2]=0
    [3]=0
    ????????
    char[] ch = bin.toCharArray();

    you can use it as you want .....

    The best possible way is as described above by using a for loop ....


    if you want the reverse of the string and then to array .... then use a StringBuilder and its reverse function and then to char Array...

    Lets be more specific so that guys in here can help u out :)

    warm regards
    Vinod M
    Last edited by Vinod Mukundan; 12-03-2010 at 06:32 AM.
    _______________________________________________
    give me beans .........

Similar Threads

  1. Binary
    By THEAniKan in forum Entertainment
    Replies: 1
    Last Post: 01-06-2012, 11:14 AM
  2. "Array Map" with Binary Search...
    By kreyszig in forum Advanced Java
    Replies: 5
    Last Post: 10-14-2010, 03:23 PM
  3. Decimal to Binary "Using Array"
    By pinkdreammsss in forum Java Applets
    Replies: 10
    Last Post: 04-23-2010, 07:21 PM
  4. Replies: 2
    Last Post: 10-07-2009, 07:24 PM
  5. get binary value
    By rushenas in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 02:51 PM

Posting Permissions

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