简体   繁体   中英

Addition of two binary numbers

I tried to compere each number by putting the numbers in char arrays and compere them each by each with if conditions. Each outcome should be covered and each outcome should be saved in String result but the result of the whole operation is always blank. Java debugger isn't working and I don´t see why it isn't working.

import java.util.Scanner;

public class BinaryAdder {
    public static String add(String binary1, String binary2) {

        String result = "";
        char[] safea = binary1.toCharArray();
        char[] safeb = binary2.toCharArray();

        int lb1 = binary1.length() - 1;
        int lb2 = binary2.length() - 1;
        char reminder = 0;

        while (lb1 != 0 || lb2 != 0) {
            if (safea[lb1] == 0 && safeb[lb2] == 0 && reminder == 0) {
                result += "0";
                lb1--;
                lb2--;
            } else if (safea[lb1] == 1 && safeb[lb2] == 0 && reminder == 0) {
                result += "1";
                lb1--;
                lb2--;
            } else if (safea[lb1] == 1 && safeb[lb2] == 1 && reminder == 0) {
                result += "0";
                reminder = 1;
                lb1--;
                lb2--;
            } else if (safea[lb1] == 1 && safeb[lb2] == 1 && reminder == 1) {
                result += "1";
                reminder = 1;
                lb1--;
                lb2--;
            } else if (safea[lb1] == 1 && safeb[lb2] == 0 && reminder == 1) {
                result += "0";
                reminder = 1;
                lb1--;
                lb2--;
            } else if (safea[lb1] == 0 && safeb[lb2] == 1 && reminder == 1) {
                result += "0";
                reminder = 1;
                lb1--;
                lb2--;
            } else if (safea[lb1] == 0 && safeb[lb2] == 1 && reminder == 0) {
                result += "1";
                lb1--;
                lb2--;
            } else if (safea[lb1] == 0 && safeb[lb2] == 0 && reminder == 1) {
                result += "1";
                lb1--;
                lb2--;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Summand:  ");
        String input1 = scan.next("(0|1)*");
        System.out.print("Summand:  ");
        String input2 = scan.next("(0|1)*");
        scan.close();
        System.out.println("Result: " + add(input1, input2));
    }

}

You can parse the bits into integer with Integer.parseInt(s, radix) . You need to use radix of 2:

public static String add(String binary1, String binary2) {
    int i1 = Integer.parseInt(binary1, 2);
    int i2 = Integer.parseInt(binary2, 2);
    return Integer.toBinaryString(i1 + i2);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM