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 2 of 2

Thread: the shift left or shift right operator problem

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default the shift left or shift right operator problem

    Write a program that, given two binary numbers represented as strings, prints their sum in binary. The binary strings are comma separated, two per line. The final answer should not have any leading zeroes. In case the answer is zero, just print one zero i.e. 0

    Input:
    Your program should read lines from standard input. Each line contains two binary strings, separated by a comma and no spaces.

    Output:
    For each pair of binary numbers print to standard output their binary sum, one per line.

    THIS IS THE INPUT:
    input 1: 110011,1010
    input 2: 11010,00101001

    THIS IS MY OUTPUT
    out put 1: 111010
    output 2: 01011011

    THIS IS A DESIRE OUTPUT
    output1: 111101
    output2: 1000011


    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.nio.charset.StandardCharsets;
     
    public class Main {
      /**
       * Iterate through each line of input.
       */
      public static void main(String[] args) throws IOException {
        InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
        BufferedReader in = new BufferedReader(reader);
        StringBuilder sb = new StringBuilder();
        String line;
        int i = 0;
        int j = 1;
        int sum;
        int carry = 0;
        int st_length1 = 0;
        int st_length2 = 0;
        String one = "";
        String two ="";
        while ((line = in.readLine()) != null) {
          //System.out.println(line);
          String [] temp = line.split(",");
          one = new String( temp[0]);
          two = new String(temp[1]);
        }
        st_length1 = one.length() - 1;
        st_length2 = two.length() - 1;
        if(one == null || two == null)
        {
          return;
        }
        while(st_length1 > 0 || st_length2 > 0)
        {
          sum = carry;
          if (st_length1 > 0)
          {
            sum += one.charAt(st_length1) - '0';
            st_length1--;
          }
          if(st_length2 > 0)
          {
            sum += two.charAt(st_length2) - '0';
            st_length2--;
          }
          carry = sum >> 1;
          sum = sum & 1;
          sb.append(sum == 0 ? '0' : '1');
          if (carry > 0)
            sb.append('1');
     
        sb.reverse();
        }
        System.out.println(sb.toString());
      }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: the shift left or shift right operator problem

    Not sure why the code is converting the String's char to int values.
    Why not just work with the String values?
    The add code takes two binary digits and a carry value and computes a new binary digit and a carry value.
    For example:
    "1" + "1" w/o carry gives "0" and a carry
    "1" + "1" with carry gives "1" and a carry
    "1" + "0" w/o carry gives "1" and no carry
    etc

    How do you do it with paper and pencil?
    Start at the rightmost digit and work left.

    Also posted here: https://coderanch.com/t/690729/java/shift-operator

    http://www.javaprogrammingforums.com...s-posting.html
    https://coderanch.com/wiki/660346/Wi...-Posting-Sites
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Rotate Left bitwise shift
    By keepStriving in forum Java Theory & Questions
    Replies: 1
    Last Post: May 13th, 2014, 07:27 PM
  2. Issue with Caeser Shift, Transpose, and Reverser
    By biwot in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2014, 06:31 AM
  3. java shift and ~ operator when to use each one?
    By tonu in forum Java Theory & Questions
    Replies: 1
    Last Post: December 27th, 2013, 12:08 PM
  4. deleting with shift ?
    By keep smiling in forum Collections and Generics
    Replies: 4
    Last Post: February 26th, 2012, 04:05 PM
  5. How to shift a String in a rectangle which is type of Canvas
    By elenora in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: April 4th, 2011, 07:39 AM