Having trouble adding 2 binary numbers in c++

This is a discussion on Having trouble adding 2 binary numbers in c++ within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble adding 2 binary numbers in C++, instead of having the user input the binary numbers into the ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    Having trouble adding 2 binary numbers in c++

    I'm having trouble adding 2 binary numbers in C++, instead of having the user input the binary numbers into the array (which I've gotten down) I shortened it to use two predetermined numbers 1010 + 0110, which should total to 10000, but the result I'm getting is 0012FEB0. Could anyone tell me where I'm going wrong with this?
    Thanks in advance for any help.

    Code:
    #include <iostream>
    using namespace std;
    
    int b2_addition (int a[], int b[]);
    int main (void)
    {int a[4]={1,0,1,0}, b[4]={0,1,1,0}, c[8];
    
    b2_addition(a, b);
    cout<<c;
    
    return(0);
    }
    int b2_addition (int a[], int b[])
    {int c[8], n_bits=4, carry=0;
     for(int i=0;i<n_bits;i++)
        {int carry=0;
         c[i]=a[i]+b[i]+carry;
             if(c[i]==3)
             {c[i]=1; carry=1;}
             if(c[i]==2)    
             {c[i]=0; carry=1;}
             if(c[i]==1)
             {c[i]=1; carry=0;}
             if(c[i]==0)
             {c[i]=0; carry=0;}
        }
    if(carry==1)
    {n_bits++; c[n_bits]=1;}
        
    return(c[8]);
    }
    Last edited by Mizzrym; 11-12-2012 at 04:22 PM.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    ok I've found one mistake looking at this in the bottom of the sub routine
    Code:
    if(carry==1)
    {n_bits++; c[n_bits]=1;
    should be
    Code:
    if(carry==1)
    {n_bits++; c[n_bits-1]=1;
    because the array starts at 0 not 1, but I'm still coming up with the same result for the total.....

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    I would make a new function with the following parameters at a minimum.

    void b2_addition (int *c, int *a, int *b);

    You will add binary numbers in the normal fashion, but store the answer in c. Use your knowledge of arrays to carry you through.

    There is no problem returning an integer exactly, but you will have to correctly copy the answer you would get into an integer variable, by say, moving 1s into the correct place with bitwise operators. I view storing the answer in an array as consistent with how you want to use the function, though.
    Last edited by whiteflags; 11-12-2012 at 05:57 PM. Reason: grammar

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding two binary numbers
    By renz15 in forum C Programming
    Replies: 58
    Last Post: 08-05-2011, 07:46 AM
  2. Having Some Trouble With Adding A Library?
    By Larmid in forum C Programming
    Replies: 7
    Last Post: 03-06-2011, 03:55 PM
  3. Trouble adding source filter for WMV
    By abachler in forum Windows Programming
    Replies: 2
    Last Post: 07-03-2008, 08:38 AM
  4. trouble adding content to string
    By letsgetaway in forum C++ Programming
    Replies: 9
    Last Post: 01-20-2008, 11:54 AM
  5. Trouble Adding to ComboBox (*not* an LB_ issue..)
    By roblarky in forum Windows Programming
    Replies: 1
    Last Post: 07-18-2007, 11:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21