Adding binary numbers in C++

2019-01-22 15:44发布

How would I add two binary numbers in C++? What is the correct logic?

Here is my effort, but it doesn't seem to be correct:

#include <iostream>
using namespace std;
int main()
{
    int a[3];
    int b[3];
    int carry = 0;
    int result[7];

    a[0] = 1;
    a[1] = 0;
    a[2] = 0;
    a[3] = 1;

    b[0] = 1;
    b[1] = 1;
    b[2] = 1;
    b[3] = 1;

    for(int i = 0; i <= 3; i++)
    {
        if(a[i] + b[i] + carry == 0)
        {
            result[i] = 0;
            carry = 0;
        }

        if(a[i] + b[i] + carry == 1)
        {
            result[i] = 0;
            carry = 0;
        }

        if(a[i] + b[i] + carry == 2)
        {
            result[i] = 0;
            carry = 1;
        }

        if(a[i] + b[i] + carry > 2)
        {
            result[i] = 1;
            carry = 1;
        }
    }
    for(int j = 0; j <= 7; j++)
    {
        cout<<result[j]<<" ";
    }
    system("pause");
}

标签: c++ binary add
12条回答
Juvenile、少年°
2楼-- · 2019-01-22 16:13
#include <stdio.h>



int main()

{



    long binary1, binary2;

    int i = 0, remainder = 0, sum[20];



    printf("Enter the first binary number: ");

    scanf("%ld", &binary1);

    printf("Enter the second binary number: ");

    scanf("%ld", &binary2);

    while (binary1 != 0 || binary2 != 0)

    {

        sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;

        remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;

        binary1 = binary1 / 10;

        binary2 = binary2 / 10;

    }

    if (remainder != 0)

        sum[i++] = remainder;

    --i;

    printf("Sum of two binary numbers: ");

    while (i >= 0)

        printf("%d", sum[i--]);

    getch();
    return 0;

}
查看更多
一纸荒年 Trace。
3楼-- · 2019-01-22 16:14

There is a bug :

if(a[i]+b[i]+carry==1)  
{   
result[i]=1; 
carry=0;  
}  

Also u might want to print in reverse

for(int j=6; j>=0; j--)  
{  
   cout<<result[j]<<" ";  
}
查看更多
我想做一个坏孩纸
4楼-- · 2019-01-22 16:14

You could use "Bitwise OR" operation to reduce the code since

1 or 1 = 1
1 or 0 = 1
0 or 1 = 1
0 or 0 = 0

You could also convert both number to decimal sum and them go back to binary again.

Converting decimal to binary

int toBinary (unsigned int num, char b[32])
    {
    unsigned  int x = INT_MIN;      // (32bits)
    int i = 0, count = 0;
    while (x != 0)
    {
      if(x & num) // If the actual o bit is 1 & 1 = 1 otherwise = 0
      {
          b[i] = '1';
          count++;
      }
      else b[i] = '0';

      x >>=1;       // pass to the left
      i++;          
    }
    return count;
    }
查看更多
混吃等死
5楼-- · 2019-01-22 16:16

you should do this

for(int i = 3; i >= 0; i--)
    {
        if(a[i] + b[i] + carry == 0)
        {
            result[i] = 0;
            carry = 0;
        }
        else if(a[i]+b[i]+carry==1)
        {
            result[i]=1;
            carry=0;  
        }
        else if(a[i] + b[i] + carry == 2)
        {
            result[i] = 0;
            carry = 1;
        }
        else if(a[i] + b[i] + carry > 2)
        {
            result[i] = 1;
            carry = 1;
        }
        printf("%d",result[i]);
    }
查看更多
不美不萌又怎样
6楼-- · 2019-01-22 16:24

A non-conventional solution, but it works:

int main() {

  int A[] = { 0, 0, 0, 1, 1, 0, 1, 0};
  int B[] = { 0, 0, 0, 0, 1, 1, 0, 0};

  int size = sizeof(A)/sizeof(*A);

  int C[size+1];
  int t = 0;

  for(int i = size-1; i > -1; i--){

      C[i+1] = A[i]+B[i]+t;
      t = C[i+1]/2;
      C[i+1] %= 2;
  }

  C[0] = t;
}
查看更多
虎瘦雄心在
7楼-- · 2019-01-22 16:27

What if their sizes are not the same? Also, you would want to allow the user to input the binary numbers (in this case representing integers) as integers and not as elements of arrays. Here is a piece of code that accomplishes those :-)

#include <iostream>
using namespace std; 

// Add two numbers in binary

void sumBinary(int num1, int num2, int* sum12){
    int mod1 = 0;
    int mod2 = 0;
    int carry = 0;
    int factor = 1;

    int flag = 0;

    *sum12 = 0;

    while (!flag){
        mod1 = num1 % 10;
        mod2 = num2 % 10;

        num1 /= 10;
        num2 /= 10;
        if ((carry + mod1 + mod2) == 2){
            *sum12 += 0;
            carry = 1;
        }
        else if ((carry + mod1 + mod2) == 3){
            *sum12 += factor;
            carry = 1;
        }
        else if ((carry + mod1 + mod2) == 0){
            *sum12 += 0;
            carry = 0;
        }
        else{
            *sum12 += factor;
            carry = 0;
        }
        factor *= 10;
        if ((num1 == 0) && (num2 == 0)){ 
            *sum12 += carry*factor;
            flag = 1; }


    }
}
void main(){
    int num1, num2, sum12;

    cout << "Enter the first binary integer number: ";
    cin >> num1;
    cout << "Enter the second binary integer number: ";
    cin >> num2;

    sumBinary(num1, num2, &sum12);

    cout << "The sum in binary form is :" << sum12 << endl;
}
查看更多
登录 后发表回答