Hi Nik,
I finally worked out how to add two one's complement binary numbers. the topic "two'sComplement binary numbers" i posted can integrate the carry functionality in my two's complement application: Below, the one's complement application:
#include <iostream>
#define SIZE 8
using namespace std;
void oneComplement(int binary_one[], int binary_two[], int result[])
{
int temp;
int count;
int carry;
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
cout << endl << carry << endl;
if ( carry == 1 )
{
count = 7;
while ( result[count] == 1 && count >= 0 )
{
result[count] = 0;
count--;
}
result[count] = 1;
}
if ( binary_one[0] == binary_two[0] &&
binary_one[0] != result[0] )
{
cout << "Overflow" << endl;
}
else
{
for ( count = 0; count < SIZE; count++ )
{
cout << result[count];
}
cout << endl;
}
} // oneComplement()
int main()
{
char binary1[SIZE+1];
char binary2[SIZE+1];
int binnum_one[SIZE];
int binnum_two[SIZE];
int result[SIZE];
int count;
cout << "Enter numbers to add> \n";
cin >> binary1;
cin >> binary2;
cout << endl;
for (count = 0; count < SIZE; count++)
{
if (binary1[count] == '0')
binnum_one[count] = 0;
else
binnum_one[count] = 1;
if (binary2[count] == '0')
binnum_two[count] = 0;
else
binnum_two[count] = 1;
} // end for
oneComplement(binnum_one, binnum_two, result);
return 0;
} // end main
George N. Bilios
|