Adding binary numbers

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

    You can just put 0b in front of the binary number to specify that it is binary.

    For this example, you can simply do:

    Integer.toString(0b1010 + 0b10, 2);
    

    This will add the two in binary, and Integer.toString() with 2 as the second parameter converts it back to binary.

    0 讨论(0)
  • 2020-11-28 08:05

    Another interesting but long approach is to convert each of the two numbers to decimal, adding the decimal numbers and converting the answer obtained back to binary!

    0 讨论(0)
  • 2020-11-28 08:05

    i tried to make it simple this was sth i had to deal with with my cryptography prj its not efficient but i hope it

        public String binarysum(String a, String b){
        int carry=0;
        int maxim;
        int minim;
        maxim=Math.max(a.length(),b.length());
        minim=Math.min(a.length(),b.length());         
        char smin[]=new char[minim];
        char smax[]=new char[maxim];
        if(a.length()==minim){
         for(int i=0;i<smin.length;i++){
         smin[i]=a.charAt(i);
          }
          for(int i=0;i<smax.length;i++){
           smax[i]=b.charAt(i);
          }
          }
          else{
              for(int i=0;i<smin.length;i++){
              smin[i]=b.charAt(i);
                 }
           for(int i=0;i<smax.length;i++){
           smax[i]=a.charAt(i);
          } 
          }
        char[]sum=new char[maxim];
        char[] st=new char[maxim];
        for(int i=0;i<st.length;i++){
        st[i]='0';
        }
        int k=st.length-1;
       for(int i=smin.length-1;i>-1;i--){
        st[k]=smin[i];
        k--;
        } 
    
         //   *************************** sum begins here
       for(int i=maxim-1;i>-1;i--){
       char x= smax[i];
       char y= st[i];
        if(x==y && x=='0'){
             if(carry==0)
                 sum[i]='0';
             else if(carry==1){
                 sum[i]='1';
                 carry=0;
        }
       }
        else if(x==y && x=='1'){
            if(carry==0){
                sum[i]='0';
                carry=1;
            }
            else if(carry==1){
              sum[i]='1';
              carry=1;
            }
         }
         else if(x!=y){
            if(carry==0){
                sum[i]='1';
                }
            else if(carry==1){
              sum[i]='0';
              carry=1;
            }
           }        }
          String s=new String(sum);
         return s;
          }
    
    0 讨论(0)
  • 2020-11-28 08:05

    I've actually managed to find a solution to this question without using the stringbuilder() function. Check this out:

    public void BinaryAddition(String s1,String s2)
    {
        int l1=s1.length();int c1=l1;
        int l2=s2.length();int c2=l2;
        int max=(int)Math.max(l1,l2);
        int arr1[]=new int[max];
        int arr2[]=new int[max];
        int sum[]=new int[max+1];
        for(int i=(arr1.length-1);i>=(max-l1);i--)
        {
            arr1[i]=(int)(s1.charAt(c1-1)-48);
            c1--;
        }
        for(int i=(arr2.length-1);i>=(max-l2);i--)
        {
            arr2[i]=(int)(s2.charAt(c2-1)-48);
            c2--;
        }
        for(int i=(sum.length-1);i>=1;i--)
        {
            sum[i]+=arr1[i-1]+arr2[i-1];
            if(sum[i]==2)
            {
                sum[i]=0;
                sum[i-1]=1;
            }
            else if(sum[i]==3)
            {
                sum[i]=1;
                sum[i-1]=1;
            }
        }
        int c=0;
        for(int i=0;i<sum.length;i++)
        {
            System.out.print(sum[i]);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 08:06

    you can write your own One.

    long a =100011111111L;
    long b =1000001111L;
    
    int carry = 0 ;
    long result = 0;
    
    long multiplicity = 1;
    
    while(a!=0 || b!=0 || carry ==1){
        if(a%10==1){
            if(b%10==1){
                result+= (carry*multiplicity);
                carry = 1;
            }else if(carry == 1){
                carry = 1;  
            }else{
                result += multiplicity;
            }
        }else if (b%10 == 1){
            if(carry == 1){
                carry = 1;
            }else {
                result += multiplicity; 
            }
        }else {
            result += (carry*multiplicity);
            carry = 0;
        }
    
        a/=10;
        b/=10;
        multiplicity *= 10;
    
    }
    
    
    System.out.print(result);
    

    it works just by numbers , no need string , no need SubString and ...

    0 讨论(0)
  • 2020-11-28 08:07

    The original solution by Martijn will not work for large binary numbers. The below code can be used to overcome that.

    public String addBinary(String s1, String s2) {
        StringBuilder sb = new StringBuilder();
        int i = s1.length() - 1, j = s2.length() -1, carry = 0;
        while (i >= 0 || j >= 0) {
            int sum = carry;
            if (j >= 0) sum += s2.charAt(j--) - '0';
            if (i >= 0) sum += s1.charAt(i--) - '0';
            sb.append(sum % 2);
            carry = sum / 2;
        }
        if (carry != 0) sb.append(carry);
        return sb.reverse().toString();
    }
    
    0 讨论(0)
提交回复
热议问题