# Combinational Logic

Using the three input, two output, logic device we designed last time we can now make a 9 input, 5 output, logic device to add two binary numbers together:

The logic circuit from lecture 5 can be drawn as a single block called a full adder.

If four of these devices are cascaded together, then we have a four bit adder.

```#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 pr(int p[8]) {
int i,no=0;
for(i=0;i<8;i++) {
printf("%d",p[i]);
no=no<<1;
if(p[i])no++;
}
printf(" (%d)\n",no);
}
void main() {
int a[8]={1,0,0,1,0,1,0,0},b[8]={0,0,1,1,0,1,1,0};
int r[8]={0,0,0,0,0,0,0,0};
int i,c=0;

for(i=7;i>=0;i--) {
fa(a[i],b[i],c,&r[i],&c);
}
pr(a);
pr(b);
printf("--------\n");
pr(r);
} ```

Subtraction can be performed in a similar way.

An adder can be used for subtraction because A-B == A+(-B). To get -B, complement B (using NOT gates) and add one to the result (two's complement). Adding one to the result can be done by making C0 = 1.

Inside the central processing unit of a computer there will be a device that performs a number of arithmetic and logic functions. E.g. Add,Sub,And,Or,Xor,Not.

This is called the Arithmetic Logic Unit or ALU.

A, B and S are n bit numbers. F is a binary code to indicate the function to be performed. O is status output from the operation, including Carry, Overflow etc..

## Sequential Logic

So far we have looked at logic circuits that have a direct mapping from input to output. Every output is just a logical combination of the input bits. This is called combinational (or combinatorial) logic.

If we allow a device to have an output that depends not only on the present input but on previous inputs (or outputs) then it is called sequential logic.

Now the output depends on the present input and the state. The box contains a combinational logic circuit.

This device can have a "memory", and we can use it to store bits.

It can also be used to control a complex sequence of operations.