Adding binary numbers

后端 未结 21 1130
孤独总比滥情好
孤独总比滥情好 2020-11-28 07:50

Does anyone know how to add 2 binary numbers, entered as binary, in Java?

For example, 1010 + 10 = 1100.

相关标签:
21条回答
  • 2020-11-28 08:17
    Try this, tested with binary and decimal and its self explanatory
    
    public String add(String s1, String s2, int radix){
            int s1Length = s1.length();
            int s2Length = s2.length();
            int reminder = 0;
            int carry = 0;
            StringBuilder result = new StringBuilder();
            int i = s1Length -1;
            int j = s2Length -1;
            while (i >=0 && j>=0) {
                int operand1 = Integer.valueOf(s1.charAt(i)+"");
                int operand2 = Integer.valueOf(s2.charAt(j)+"");
                reminder = (operand1+operand2+carry) % radix;
                carry = (operand1+operand2+carry) / radix;
                result.append(reminder);
                i--;j--;
            }
            while(i>=0){
                int operand1 = Integer.valueOf(s1.charAt(i)+"");
                reminder = (operand1+carry) % radix;
                carry = (operand1+carry) / radix;
                result.append(reminder);
                i--;
            }
            while(j>=0){
                int operand1 = Integer.valueOf(s2.charAt(j)+"");
                reminder = (operand1+carry) % radix;
                carry = (operand1+carry) / radix;
                result.append(reminder);
                j--;
            }
            return result.reverse().toString();
        }
    }
    
    0 讨论(0)
  • 2020-11-28 08:22
    public String addBinary(String a, String b) { 
        int carry = 0;
        StringBuilder sb = new StringBuilder();
        for(int i = a.length() - 1, j = b.length() - 1;i >= 0 || j >= 0;i--,j--){
            int sum = carry + (i >= 0 ? a.charAt(i) - '0':0) + (j >= 0 ? b.charAt(j) - '0' : 0);
            sb.append(sum%2);
            carry =sum / 2;
        }
        if(carry > 0) sb.append(carry);
        sb.reverse();
        return sb.toString();
    }
    
    0 讨论(0)
  • 2020-11-28 08:24

    To dive into fundamentals:

    public static String binaryAddition(String s1, String s2) {
        if (s1 == null || s2 == null) return "";
        int first = s1.length() - 1;
        int second = s2.length() - 1;
        StringBuilder sb = new StringBuilder();
        int carry = 0;
        while (first >= 0 || second >= 0) {
            int sum = carry;
            if (first >= 0) {
                sum += s1.charAt(first) - '0';
                first--;
            }
            if (second >= 0) {
                sum += s2.charAt(second) - '0';
                second--;
            }
            carry = sum >> 1;
            sum = sum & 1;
            sb.append(sum == 0 ? '0' : '1');
        }
        if (carry > 0)
            sb.append('1');
    
        sb.reverse();
        return String.valueOf(sb);
    }
    
    0 讨论(0)
  • 2020-11-28 08:24

    Java solution

    static String addBinary(String a, String b) {
    
        int lenA = a.length();
        int lenB = b.length();
        int i = 0;
    
        StringBuilder sb = new StringBuilder();
        int rem = Math.abs(lenA-lenB);
        while(rem >0){
            sb.append('0');
            rem--;
        }
        if(lenA > lenB){
            sb.append(b);  
            b = sb.toString();
        }else{
            sb.append(a);
            a = sb.toString();
        }
    
        sb = new StringBuilder();
        char carry = '0';
        i = a.length();
        while(i > 0){
            if(a.charAt(i-1) == b.charAt(i-1)){
                sb.append(carry);
                if(a.charAt(i-1) == '1'){
                    carry = '1';
                }else{
                    carry = '0';
                }
            }else{
                if(carry == '1'){
                    sb.append('0');
                    carry = '1';
                }else{
                    carry = '0';
                    sb.append('1');
                }
            }
            i--;
        }
    
        if(carry == '1'){
            sb.append(carry);
        }
    
        sb.reverse();
        return sb.toString();
    
    }
    
    0 讨论(0)
  • 2020-11-28 08:24
    public class BinaryArithmetic {
    
        /*-------------------------- add ------------------------------------------------------------*/
        static String add(double a, double b) {
            System.out.println(a + "first val :" + b);
            int a1 = (int) a;
            int b1 = (int) b;
            String s1 = Integer.toString(a1);
            String s2 = Integer.toString(b1);
            int number0 = Integer.parseInt(s1, 2);
            int number1 = Integer.parseInt(s2, 2);
    
            int sum = number0 + number1;
            String s3 = Integer.toBinaryString(sum);
    
            return s3;
        }
        /*-------------------------------multiply-------------------------------------------------------*/
    
        static String multiply(double a, double b) {
            System.out.println(a + "first val :" + b);
            int a1 = (int) a;
            int b1 = (int) b;
            String s1 = Integer.toString(a1);
            String s2 = Integer.toString(b1);
            int number0 = Integer.parseInt(s1, 2);
            int number1 = Integer.parseInt(s2, 2);
    
            int sum = number0 * number1;
            String s3 = Integer.toBinaryString(sum);
    
            return s3;
        }
        /*----------------------------------------substraction----------------------------------------------*/
    
        static String sub(double a, double b) {
            System.out.println(a + "first val :" + b);
            int a1 = (int) a;
            int b1 = (int) b;
            String s1 = Integer.toString(a1);
            String s2 = Integer.toString(b1);
            int number0 = Integer.parseInt(s1, 2);
            int number1 = Integer.parseInt(s2, 2);
    
            int sum = number0 - number1;
            String s3 = Integer.toBinaryString(sum);
    
            return s3;
        }
    
        /*--------------------------------------division------------------------------------------------*/
        static String div(double a, double b) {
            System.out.println(a + "first val :" + b);
            int a1 = (int) a;
            int b1 = (int) b;
            String s1 = Integer.toString(a1);
            String s2 = Integer.toString(b1);
            int number0 = Integer.parseInt(s1, 2);
            int number1 = Integer.parseInt(s2, 2);
    
            int sum = number0 / number1;
            String s3 = Integer.toBinaryString(sum);
    
            return s3;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 08:24
    import java.util.*;
    public class BitAddition {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner sc = new Scanner(System.in);
            int len = sc.nextInt();
            int[] arr1 = new int[len];
            int[] arr2 = new int[len];
            int[] sum = new int[len+1];
            Arrays.fill(sum, 0);
            for(int i=0;i<len;i++){
                arr1[i] =sc.nextInt();
            }
            for(int i=0;i<len;i++){
                arr2[i] =sc.nextInt();
            }
            for(int i=len-1;i>=0;i--){
                if(sum[i+1] == 0){
                    if(arr1[i]!=arr2[i]){
                        sum[i+1] = 1;
                    }
                    else if(arr1[i] ==1 && arr2[i] == 1){
                        sum[i+1] =0 ;
                        sum[i] = 1;
                    }
                }
                else{
                    if((arr1[i]!=arr2[i])){
                        sum[i+1] = 0;
                        sum[i] = 1;
                    }
                    else if(arr1[i] == 1){
                        sum[i+1] = 1;
                        sum[i] = 1;
                    }
                }
            }
            for(int i=0;i<=len;i++){
    
            System.out.print(sum[i]);
            }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题