954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Adding two binary numbers and find over flow bits

#include <stdio.h>
#include <iomanip>


int main()
{

	int binary1[8] = {0,1,1,1,1,0,1,1}; //8 element array
	int binary2[8] = {1,0,1,0,1,1,1,1}; //8 element array
	int binarySum[9];
	int overflow [9];

	int i;

	for (i=0; i<9; i++) overflow[i] = 0;
	for (i=9; i >= 0; i--)
		{
			binarySum[i] = binary1[i] + binary2[i] + overflow[i];
			if ( binarySum[i] > 1)
				{
					overflow[i-1] = 1;
					binarySum[i] %= 2;
				}
		}
	

	printf("Binary Sum is: ");
	for (i = 0; i<9; i++) printf("%i", binarySum[i]);
	printf("\n");
	printf("\n");
	printf("Carry Bit is: ");
	for (i = 8 ; i>=0 ; i--) printf("%i", overflow[i]);
	printf("\n");

	return (0);

}


my output is:
Binary Sum is: 00101010-1717986920

Carry Bit is: 001111111


-I'm getting debug error message.
-why my 9th bit is long negative number on binary sum?
I should get "100101010".
-carry bits should be "011111111" but I'm getting "001111111".

any help~~

jack223
Light Poster
27 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

LOoks like your concept of array indexing is a bit shaky.
If the array is declared as int binary[10], it contains 10 elements which can be accessed using binary[0] to binary[9].

If you use binary[10] you would be handling memory which doest belong to you and its called out of bounds exception.

for (i=9; i >= 0; i--)
        {
               binarySum[i] = binary1[i] + binary2[i] + overflow[i];

  // binarySum has indices from 0 to 8 since it has size 9
     // your very first iteration attempst to modify the 10th element
     // i.e. binarySum [9] which is not actually there.
     // the same problem is with your other arrays

            if ( binarySum[i] > 1)
                {
                    overflow[i-1] = 1;
                    binarySum[i] %= 2;
                }
        }
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

ok...i did change it to "binarySum[8]".

and if i also change

for (i=0; i<8; i++) overflow[i] = 0;
	for (i=8; i >= 0; i--)
		{
			binarySum[i] = binary1[i] + binary2[i] + overflow[i];
			if ( binarySum[i] > 1)
				{
					overflow[i-1] = 1;
					binarySum[i] %= 2;
				}
		}


how am i suppose to get the 9th bit??

binarySum should be = 100101010

jack223
Light Poster
27 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

Not sure about the theory. But here is a working code.

#include <stdio.h>
//#include <iomanip> why are you including this? You are not using it.


int main()
{

    int binary1[8] = {0,1,1,1,1,0,1,1}; //8 element array
    int binary2[8] = {1,0,1,0,1,1,1,1}; //8 element array
    int binarySum[9] = { 0 }; // Initilize to zero like this. It is much faster.
                             // The long integer appeared because you didnt initilize binarySum.
    int overflow[9] = { 0 }; // Initilize to zero like this. It is much faster.
    int i;

    for (i=7; i >= 0; i--)// Since you are adding binary1 and binary2,
                          // You should index from 7. There is no element 9 for them.
    {
        binarySum[i+1] = binary1[i] + binary2[i] + overflow[i+1];
        if ( binarySum[i+1] > 1)
        {
            if ( i == 0 )
            {
                binarySum[ i ] = 1;
            }
            overflow[i] = 1;
            binarySum[i+1] %= 2;
        }
    }

    for (i = 0; i < 8; i++) printf("%i", binary1[i]);
    printf("\n");
    for (i = 0; i < 8; i++) printf("%i", binary2[i]);
    printf("\n");
    printf("Binary Sum is: ");
    for (i = 0; i < 9; i++) printf("%i", binarySum[i]);
    printf("\n");
    printf("\n");
    printf("Carry Bit is: ");
    for (i = 8 ; i>=0 ; i--) printf("%i", overflow[i]);
    printf("\n");

    return (0);

}
WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

> how am i suppose to get the 9th bit??
There are only 8 bits.

Also, you only need ONE carry bit, not 8 separate ones.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

thanks guys for all your help~
I love this website....

jack223
Light Poster
27 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

The solution above works only on the specific 8 binary digit numbers (01111011 & 10101111). I thought i'd modify it a little and make it generic for any two N bit binary numbers. Hope whoever stumbles upon this finds it useful. Again, you could make the user input the binary numbers himself for more robustness, but you must get the picture by now. I'll leave that for yawl to edit.

(I also UNreversed the Carry Bit since i dont see why yawl was printing it in reverse. I think it's much clearer printed the way it is)

The following code should work fine:

#include <stdio.h>
#define  N  8 // Change accordingly to add any two n-bits binary numbers
int main()
{
 int binary1[N] = {0,1,1,1,1,0,1,1}; //N element array
 int binary2[N] = {1,0,1,0,1,1,1,1}; //N element array
 int binarySum[N + 1]; // No need for initialization, all will later be
 int overflow [N + 1] = {0};
 
 int i;
 for (i=N; i > 0; i--)
  {
   binarySum[i] = binary1[i-1] + binary2[i-1] + overflow[i];
   if ( binarySum[i] > 1)
    {
     overflow[i-1] = 1;
     binarySum[i] %= 2;
    }
  }
 binarySum[0] = overflow[0];
 
 printf("\n\t\t    Adding Two N-Bit Binary Numbers:\n\n\n");
 printf("Carry Bit is:    ");
 for (i = 0 ; i<=N ; i++) printf("%i", overflow[i]);
 printf("\n\nBinary1:\t  ");
 for (i = 0; i < N; i++) printf("%i", binary1[i]);
    printf("\nBinary2:\t+ ");
    for (i = 0; i < N; i++) printf("%i", binary2[i]);
    printf("\n");
 printf("\t\t  --------\nBinary Sum is:   ");
 for (i = 0; i<N+1; i++) printf("%i", binarySum[i]);
 printf("\n");
 printf("\n");
 printf("\n\n");
 getchar();
 return (0);
}
MC Moe
Newbie Poster
3 posts since Nov 2006
Reputation Points: 10
Solved Threads: 1
 
> how am i suppose to get the 9th bit?? There are only 8 bits. Also, you only need ONE carry bit, not 8 separate ones.




You're right, but then you won't be able to store the carry at each iteration. For, the program later prints them out (showing you the value of each carry at each bit addition).

MC Moe
Newbie Poster
3 posts since Nov 2006
Reputation Points: 10
Solved Threads: 1
 

Oh and About the 9th bit:

binarySum[0] = overflow[0]; solves the issue..

MC Moe
Newbie Poster
3 posts since Nov 2006
Reputation Points: 10
Solved Threads: 1
 
#include<stdio.h>
#define N 8
int main()
{
	int a[N]={0,1,1,1,1,1,1,1};
	int b[N]={0,1,1,1,1,1,1,1};
	int sum[N + 1]={0};
	int carry=0,i;
	for(i=N-1;i>=0;i--)
	{
		sum[i+1]=a[i] ^ b[i] ^ carry;
		carry=(a[i] & b[i]) + (carry*(a[i] ^ b[i]));
	}
	sum[0]=carry;
	printf("  a:   ");
	for(i=0;i<N;i++)
	{
		printf("%d ",a[i]);
	}
	printf("\n  b:   ");
	for(i=0;i<N;i++)
	{
		printf("%d ",b[i]);
	}
	printf("\nsum: ");
	for(i=0;i<N+1;i++)
	{
		
		printf("%d ",sum[i]);
	}
return 0;
}
sania77
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

Can I ask what's the use of i in this program? I'm trying to learn c language.

harunosakura
Newbie Poster
5 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

What if for example
int a[N]={0,1,1,1,1,1,1,1};
int b[N]={0,1,1,1,1,1,1,1}; is not your fixed value and you want to ask for two binary numbers first then add? What are you supposed to do?

harunosakura
Newbie Poster
5 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You