Page 1 of 1

## 3 Replies - 24909 Views - Last Post: 23 April 2010 - 03:04 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=167024&amp;s=82e5813dc5b48a88fd9fc7053f2b1265&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Pranobesh

Reputation: 0
• Posts: 1
• Joined: 08-April 10

Posted 08 April 2010 - 09:22 PM

```#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[8],b[8],c[9];
int i,j;
int sum,carry=0;
printf("Enter the First Eight bit number->");
for(i=0;i<8;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the Second Eight bit number->");
for(i=0;i<8;i++)
{
scanf("%d",&b[i]);
}
j=8;
for(i=7;i>=0;i--)
{
if(a[i]==b[i]==1)
{
sum=0;
carry=1;
}
else
{
if(a[i]==b[i]==0)
{
sum=0;
carry=0;
}
else
{
sum=1;
carry=0;
}
}
if(sum==carry==1)
{
sum=0;
carry=1;
}
else
{
if(sum==carry==0)
{
sum=0;
carry=0;
}
else
{
sum=1;
carry=0;
}
}
c[j]=sum;
j--;
}
printf("\nthe sum of the two binary number is->");
for(j=8;j>=0;j--)
{
printf("%d",c[j]);
}
getch();
}
```

This post has been edited by JackOfAllTrades: 23 April 2010 - 04:57 AM
Reason for edit:: Added code tags.

Is This A Good Question/Topic? 0

## Replies To: 8 bit binary addition

### #2 bodom658

• Villiage Idiom

Reputation: 113
• Posts: 1,123
• Joined: 22-February 08

## Re: 8 bit binary addition

Posted 08 April 2010 - 09:46 PM

Do you have a question?

I would personally do this:

Say I have array A, B, and the result array C.

I would say that each are 8 bit, and the 0 index is the 0 position (lowest significant bit) in the binary number.

So, what do we do? We iterate through each set of bits. XOR them, and then XOR the result with the carry if there is one. Then set this value to the box in C. Then, AND them, and set that value to carry.

```unsigned char A[8], B[8], C[8], carry = 0;

// TODO: Set values of arrays

int i;
for(i = 7; i >= 0; i--) {
C[i] = (A[i] ^ B[i]) ^ carry;
carry = A[i] & B[i];
}

for(i = 0; i < 8; i++) {
printf("%d ", C[i] + 0x30);
}

printf("\n");
// Done.

```

EDIT: fixed small error, submitted general version (better optimization and user sets number of bits) as function to snippets

This post has been edited by bodom658: 08 April 2010 - 10:10 PM

### #3 johnmatthais

• D.I.C Regular

Reputation: 12
• Posts: 271
• Joined: 21-January 09

## Re: 8 bit binary addition

Posted 08 April 2010 - 09:55 PM

Nevermind, looks like I misinterpreted. You're asking for a code check, huh?

This post has been edited by johnmatthais: 08 April 2010 - 09:56 PM

Reputation:

## Re: 8 bit binary addition

Posted 23 April 2010 - 03:04 AM

This is what ....which is working for me......

```#include<stdio.h>
#include<conio.h>

int main(int argc, char* argv[])
{
int a[8],b[8],c[9], carry=0;
int i,j;
//int sum;
printf("Enter the First Eight bit number->");
for(i=0;i<8;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the Second Eight bit number->");
for(i=0;i<8;i++)
{
scanf("%d",&b[i]);
}

j=8;
for(i=7;i>=0;i--)
{
if (a[i]==1 && b[i]==1 && carry ==1)
{
c[j] = 1;
carry =1;
}
else if (a[i]==1 && b[i]==1 && carry ==0)
{
c[j]=0;
carry=1;
}
else if (a[i]==0 && b[i]==1 && carry ==0)
{
c[j]=1;
carry=0;
}
else if (a[i]==0 && b[i]==1 && carry ==1)
{
c[j]=0;
carry=1;
}
else if (a[i]==1 && b[i]==0 && carry ==1)
{
c[j]=0;
carry=1;
}
else if (a[i]==1 && b[i]==0 && carry ==0)
{
c[j]=1;
carry=0;
}
else if (a[i]==0 && b[i]==0 && carry ==1)
{
c[j]=1;
carry=0;
}
else if (a[i]==0 && b[i]==0 && carry ==0)
{
c[j]=0;
carry=0;
}

j--;

}
c[0] = carry;

printf("\nthe sum of the two binary number is->");
for(j=0;j<=8;j++)
{
printf("%d",c[j]);
}
getch();
return 0;
}
```

This post has been edited by JackOfAllTrades: 23 April 2010 - 04:57 AM
Reason for edit:: Added code tags.