Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 15th, 2004, 12:20 AM
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
  #2 (permalink)  
Old March 15th, 2004, 02:05 AM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Not a bad beginning, but:


You are using cin to read characters into arrays. I assume you are entering eight '0' or '1's for each number. The function cin reads them as strings (it takes 9 locations to hold an eight-character string).

Change declarations for bin_one and bin_two to [SIZE+1].

(This isn't a really great way to get the thingies in, but it will work if you are careful, and will let you get to the good stuff.)

Also, I think the loops in main() that convert chars to ints should
be something like
Code:
  if (bin_one[count] == '0')
            binnum_one[count] = 0;
        else
            binnum_one[count] = 1;
etc., since you use these variables in your call to the actual
arithmetic routine.

Your arithmetic routine works for some numbers, but not for others,
since you don't add the final carry back into the result.

try adding the following: -16 and -1
(one's complement 11101111 and 11111110)
the answer should be -17
(one's complement 11101110)

What do you get?

Good Luck!

Dave
Reply With Quote
  #3 (permalink)  
Old March 15th, 2004, 02:09 AM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oops, when I said that your arithmetic routine is OK except for the end-around carry, I forgot to note that the loop in oneComplement() should go from SIZE-1 all the way to 0:

Code:
for (count = SIZE - 1; count >= 0; count--)
Reply With Quote
  #4 (permalink)  
Old March 15th, 2004, 04:06 AM
Authorized User
 
Join Date: Mar 2004
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
Default

I added (one's complement 11101111 and 11111110) and got 433111......
I was expecting in the output, 11101110(Neg).

Is there an alternative way of doing this so that it will work for all numbers and situations with overflow.

George N. Bilios
Reply With Quote
  #5 (permalink)  
Old March 15th, 2004, 04:33 AM
Authorized User
 
Join Date: Mar 2004
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
Default

I get 10110111, which is 11101101 displayed reverse. 11101101 is the result before the carry.

I'm not getting 11101110, or in the proper format.

Is there a way i can fix the code so that the carry is added back into the final result and not displayed in reverse?

gbilios

George N. Bilios
Reply With Quote
  #6 (permalink)  
Old March 15th, 2004, 11:12 AM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I just made another loop, similar to your previous loops, that added the carry to each bit of the result (in the same order that you used: 7 is the index of the least significant bit, so loop from 7 downto 0.)

In your addition routine, print out the result before the loop that adds the carry, then print out the result after the carry. If you need to add print statements for each pass through the loop, do it.

Good Luck!!!!!
Dave
Reply With Quote
  #7 (permalink)  
Old March 17th, 2004, 09:12 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It might make things a little cleaner to replace this:

Quote:
quote:
       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;
to:
   binnum_one[count] = bin_one[count] - '0';
   binnum_two[count] = bin_two[count] - '0';


This works because '0' and '1' are ascii characters who's (decimal) integer values are 48 and 49. So if you want to convert the character '0' to 0, you can use integer arithmetic. Just subtract '0'. This translates to 48 - 48, which of course is equal to zero. Similar for '1'. It translates to 49 - 48, which equals 1.

Cleans up all the if statements.


Next, let's look at your logic. You should notice that your "result" is zero for even sums (0 and 2), and one for odd sums (1 and 3). You should also notice that your carry bit is only set when your sum is greater than 1 (2 and 3).

You can rewrite your entire if/else chain in two lines:

  result[count] = temp % 2; // even sums get 0, odd sums get 1.
  carry = (temp > 1)? 1 : 0; // carry if sum > 1.



My other problem is with your function declaration. Why pass in the "result" variable at all? You never use it, not really. You pass in this array of SIZE integers to the function, set one index of that array per loop iteration, then output the item you just set in the array.

This also means you've got one major flaw: You process your addition right-to-left, but you output the current result in the loop, so that's left-to-right. This is why your output is always backwards.


I suggest changing the "result" variable from a parameter to a local variable, and making it a char array to make it easier to output. We perform a simple int-to-char conversion by adding back that '0' offset.


Here's a working version of your app. Let me know if it doesn't make sense.


#include <iostream>

#define SIZE 8

using namespace std;


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

    result[SIZE] = '\0';

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

        result[count] = (temp % 2) + '0'; // even sums get 0, odds get 1.
        carry = (temp > 1)? 1 : 0; // carry if sum > 1.

    } // end for


    cout << result;

} // oneComplement()

int main()
{
    char bin_one[SIZE + 1]; // +1 to hold NULL character
    char bin_two[SIZE + 1]; // +1 to hold NULL character
    int binnum_one[SIZE];
    int binnum_two[SIZE];

    int count;

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

    for (count = 0; count < SIZE; count++)
    {
         binnum_one[count] = bin_one[count] - '0';
         binnum_two[count] = bin_two[count] - '0';
    }

   oneComplement(binnum_one, binnum_two);

   cout << "\n";
   return 0;
}



I should add that your question sounds a lot like a homework assignment. If this is the case, you should take care that you're not violating any of your school's or teacher's rules about where you can get help and/or answers to homework problems.

I don't like answering other people's homework problems...

Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #8 (permalink)  
Old March 18th, 2004, 04:54 AM
Authorized User
 
Join Date: Mar 2004
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
Default

thats understandable you don't like answering people's homework questions. i'm a novice, and thanks for giving me a kick in the right direction.

Take care,

gbilios



George N. Bilios
Reply With Quote
  #9 (permalink)  
Old March 18th, 2004, 07:11 AM
Authorized User
 
Join Date: Mar 2004
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
Reply With Quote
  #10 (permalink)  
Old March 23rd, 2004, 09:13 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Glad you got it working!

Take care,

Nik
http://www.bigaction.org/
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Hexadecimal Addition madanshah16 XSLT 1 July 31st, 2007 07:21 AM
Addition of Textboxes rhd110 General .NET 3 June 23rd, 2007 04:49 PM
Addition in javascript m_soni21 VBScript 2 December 19th, 2006 11:06 AM
addition to errata luciano991 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 0 August 25th, 2006 08:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.