Circuit model of classical computation

"(This notebook needs to input  classicgatefigs.nb)";

Circuit classical computation model of

andgate[bit1_,bit2_]=Which[bit1==0 && bit2==0,0,bit1==0 && bit2==1,0,bit1==1&&bit2==0,0,bit1==1 && bit2==1,1,1==1,"you made incorrect entry"];

Show[and[1,2]]

" Truth table for and gate";
andtruthtable=Flatten[Table[{x,y,"|",andgate[x,y]},{x,0,1},{y,0,1}],1];
TableForm[andtruthtable]

xorgate[bit1_,bit2_]=Which[bit1==0 && bit2==0,0,bit1==0 && bit2==1,1,bit1==1&&bit2==0,1,bit1==1 && bit2==1,0,1==1,"you made incorrect entry"];

xortruthtable=Flatten[Table[{x,y,"|",xorgate[x,y]},{x,0,1},{y,0,1}],1];
TableForm[xortruthtable]

Show[xor[1,2]]

orgate[bit1_,bit2_]=Which[bit1==0 && bit2==0,0,bit1==0 && bit2==1,1,bit1==1&&bit2==0,1,bit1==1 && bit2==1,1,1==1,"you made incorrect entry"];

ortruthtable=Flatten[Table[{x,y,"|",orgate[x,y]},{x,0,1},{y,0,1}],1];
TableForm[ortruthtable]

Show[or[1,2]]

Using the above gates we can form more complex logic
 gates by combining them. The halfadder circuit is shown
 below

Show[halfdraw[]]

halfadder[x0_,y0_]:={xorgate[x0,y0],andgate[x0,y0]}

" Truth table for halfadder";
halfaddertruthtable=Flatten[Table[{x,y,"|",halfadder[x,y]},{x,0,1},{y,0,1}],1];
TableForm[halfaddertruthtable]

"We can also construct a full adder";

Show[fulldraw[1,2]]

fulladder[x_,y_,c_]:=Module[{carry1},carry1=halfadder[x,y][[2]];ybit1=halfadder[x,y][[1]];ybit2=halfadder[ybit1,c][[2]];sumbit=halfadder[ybit1,c][[1]];carrybit=orgate[carry1,ybit2];{sumbit,carrybit}]

"Lets check for the integrity of the full adder ";

"x      y     c     sum/carry"

TableForm[Flatten[Table[{x,y,c,fulladder[x,y,c]},{x,0,1},{y,0,1},{c,0,1}],2]]

Show[full[1,1]]

Show[fourbitdraw]

fourbitadder[xfourbitstring_,yfourbitstring_]:=Module[{x0,x1,x2,x3,y0,y1,y2,y3},x0=xfourbitstring[[4]];x1=xfourbitstring[[3]];x2=xfourbitstring[[2]];x3=xfourbitstring[[1]];y0=yfourbitstring[[4]];y1=yfourbitstring[[3]];y2=yfourbitstring[[2]];y3=yfourbitstring[[1]];carry0=halfadder[x0,y0][[2]];sum0=halfadder[x0,y0][[1]];carry1=fulladder[x1,y1,carry0][[2]];sum1=fulladder[x1,y1,carry0][[1]];carry2=fulladder[x2,y2,carry1][[2]];sum2=fulladder[x2,y2,carry1][[1]];
carry3=fulladder[x3,y3,carry2][[2]];sum3=fulladder[x3,y3,carry2][[1]];{carry3,sum3,sum2,sum1,sum0}]

registerx={1,1,0,0};
registery={0,1,1,1};

fourbitadder[registerx,registery]

registerx={1,0,0,1};
registery={0,1,1,1};

fourbitadder[registerx,registery]

We can think of the addition of the two strings of four bit registers
as a mapping,or function

f:{0,1}   [x3,y3,x2,y2,x1,y1,x0,y0] →[z4,z3,z2,z1,z0]

Evaluation of this function is called a computation. We constructed a gate that evaluates this function through a combination of fundamental, Boolean gates that map two bits into one. The circuit model of computation holds that all classical computations can be analyzed in terms of a few basic Boolean gates.