View Single Post
  #1 (permalink)  
Old March 15th, 2004, 12:20 AM
gbilios gbilios is offline
Authorized User
 
Join Date: Mar 2004
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
Default 1'sComplement binary addition

The code below is suppose to add two 1's complement binary numbers
together using 1's complement addition rules. The numbers entered should be
exactly 8 bits long. The program reads both numbers from the command line and checks for each cahracter for zeros and ones and any carry bits.

I'm not getting the proper output from this code. Can someone please help me.

#include <iostream>

#define SIZE 8

using namespace std;

void oneComplement(int binary_one[], int binary_two[], int result[])
{
    int temp;
    int count;
    int carry = 0;

    for (count = SIZE - 1; count > 0; count--)
    {
        temp = binary_one[count] + binary_two[count] + carry;

        if (temp == 0)
        {
            result[count] = 0;
            carry = 0;
        }

        if (temp == 1)
        {
            result[count] = 1;
            carry = 0;
        }

        if (temp == 2)
        {
            result[count] = 0;
            carry = 1;
        }

        if (temp == 3)
        {
            result[count] = 1;
            carry = 1;
        }


    cout << result[count];

    } // end for


} // oneComplement()

int main()
{
    char bin_one[SIZE];
    char bin_two[SIZE];
    int binnum_one[SIZE];
    int binnum_two[SIZE];
    int result[SIZE];

    int count;

    cout << "Enter numbers to add> \n";
    cin >> bin_one;
    cin >> bin_two;

    for (count = 0; count < SIZE; count++)
    {
        if (bin_one[count] == '0')
            bin_one[count] = 0;
        else
            bin_one[count] = 1;

        if (bin_two[count] == '0')
            bin_two[count] = 0;
        else
            bin_two[count] = 1;

    } // end for

oneComplement(binnum_one, binnum_two, result);

return 0;
}
__________________
gbilios
Reply With Quote