# Lecture 5

Example: Build a device to add two numbers together - An adder:
This device will add together A and B to give S. C4 is the carry from the top bit and indicates that the result is larger than 15. C0 is the carry input; by connecting C4 from one device to C0 from another an 8 bit adder can be made.

In order to add two bits together use the following truth table

```ab|cs
--|--
00|00
01|01
10|01
11|10```
Truth Table for addition of two bits.
s is the sum and c is the carry.

s=a XOR b, c=a AND b

In order to add three bits together use the following truth table:

```cab|cs
---|-
000|00
001|01
010|01
011|10
100|01
101|10
110|10
111|11```
Truth Table
s=(a XOR b) XOR c, c=(a AND b) OR (a AND c) OR (b AND c).

Logic Diagram:

```#include <stdio.h>
void fa(int a,int b,int cin,int *sout,int *cout) {
*sout=((a^b)^cin);
*cout=((a&cin)|(a&b)|(b&cin));
}

void main() {
int a,b,c,sout,cout;

printf("_c_a_b_|_c_s_\n");
for(a=0;a<2;a++)
for(b=0;b<2;b++)
for(c=0;c<2;c++) {
fa(a,b,c,&sout,&cout);
printf(" %d %d %d | %d %d\n",c,a,b,cout,sout);
}
} ```