Adding binary numbers

后端 未结 21 1491
孤独总比滥情好
孤独总比滥情好 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:12

    One of the simple ways is as:

    1. convert the two strings to char[] array and set carry=0.
    2. set the smallest array length in for loop
    3. start for loop from the last index and decrement it
    4. check 4 conditions(0+0=0, 0+1=1, 1+0=1, 1+1=10(carry=1)) for binary addition for each element in both the arrays and reset the carry accordingly.
    5. append the addition in stringbuffer
    6. append rest of the elements from max size array to stringbuffer but check consider carry while appending
    7. print stringbuffer in reverse order for the answer.

    //The java code is as

    static String binaryAdd(String a, String b){
        int len = 0;
        int size = 0;
        char[] c1 = a.toCharArray();
        char[] c2 = b.toCharArray();
        char[] max;
    
    
        if(c1.length > c2.length){
            len = c2.length;
            size = c1.length;
            max = c1;
        }       
        else
        {
            len = c1.length;
            size = c2.length;
            max = c2;
        }
    
        StringBuilder sb = new StringBuilder();
        int carry = 0;
        int p = c1.length - 1;
        int q = c2.length - 1;
    
        for(int i=len-1; i>=0; i--){
            if(c1[p] == '0' && c2[q] == '0'){
                if(carry == 0){
                    sb.append(0);
                    carry = 0;
                }   
                else{
                    sb.append(1);
                    carry = 0;
                }   
            }
            if((c1[p] == '0' && c2[q] == '1') || (c1[p] == '1' && c2[q] == '0')){
                if(carry == 0){
                    sb.append(1);
                    carry = 0;
                }   
                else{
                    sb.append(0);
                    carry = 1;
                }                   
            }
            if((c1[p] == '1' && c2[q] == '1')){
                if(carry == 0){
                    sb.append(0);
                    carry = 1;
                }   
                else{
                    sb.append(1);
                    carry = 1;
                }
            }
            p--;
            q--;
        }
    
        for(int j = size-len-1; j>=0; j--){
            if(max[j] == '0'){ 
                if(carry == 0){     
                    sb.append(0);
                    carry = 0;
                }   
                else{
                    sb.append(1);
                    carry = 0;
                }   
            }
            if(max[j] == '1'){
                if(carry == 0){     
                    sb.append(1);
                    carry = 0;
                }   
                else{
                    sb.append(0);
                    carry = 1;
                }   
            }           
        }
        if(carry == 1)
            sb.append(1);   
        return sb.reverse().toString();
    }
    

提交回复
热议问题