Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: anyone can tell me how this code works?

  1. #1
    Junior Member
    Join Date
    Nov 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default anyone can tell me how this code works?

    public void addBinary(String a, String b) throws IOException{

    StringBuilder sb = new StringBuilder();
    int i=a.length()-1,j= b.length()-1, carry = 0;

    while (i>=0 || j>=0){
    int suma = carry;
    if(j >=0) {
    suma = suma + b.charAt(j--) - '0';
    }
    if(i >=0) {
    suma = suma + a.charAt(i--) - '0';
    }
    sb.append(suma % 2);
    carry= suma / 2;
    }
    if (carry!= 0){
    sb.append(aca);
    }
    }

    this code works perfectly. but i can understand how this logic works ( suma = suma + b.charAt(j--) - '0'; )
    can someone can explain me ? thank you folks
    sorry for my english

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: anyone can tell me how this code works?

    The line simply converts a character value of '0' or '1' to a binary value of 0 or 1 and adds it to a running sum. The sum is reset for each iteration of the loop.

    And the code shown does not work perfectly because aca is not defined and the result of adding two binary numbers ends up being reversed.

    Regards,
    Jim

  3. #3
    Junior Member
    Join Date
    Nov 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: anyone can tell me how this code works?

    thanks. this code work fine, aca is carry, i forget to change it
    thank you for the answer

  4. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: anyone can tell me how this code works?

    You're welcome but the code does not work fine. You have a void method called addBinary which computes the addition but can't return anything and doesn't print out an answer.

    Regards,
    Jim

  5. The Following User Says Thank You to jim829 For This Useful Post:

    johndoe123 (November 27th, 2018)

Similar Threads

  1. MalformedURLException error yet code still works
    By jt183 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 5th, 2014, 08:40 PM
  2. Code works in IE11 but not chrome or Firefox
    By craygo in forum Java Applets
    Replies: 5
    Last Post: August 22nd, 2014, 01:16 PM
  3. Replies: 9
    Last Post: September 15th, 2013, 02:48 PM
  4. how this code works?
    By vigneshwaran in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2012, 09:18 AM
  5. This Works In My Other Code But Not This One?
    By Ooogel in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 5th, 2012, 05:49 PM

Tags for this Thread