# find output and carry
# it is a half-adder circuit with the output 'a' and carry 'c' given by the boolean equations
import math
#Variable declaration
b1 = 1 #input-1
b2 = 1 #input-2
#Calculations
a=(b1 and (not b2))+((not b1) and b2) #sum
c=(b1 and b2) #carry
#Result
print("sum:")
print("a = %d\n"%a)
print("carry:")
print("c = %.f"%c)
# find difference and borrow
#the circuit is that of a half subtractor
import math
#Variable declaration
b1 = 1 #input-1, case-1
B1 = 0 #input-2 case-1
b2 = 1 #input-1, case 2
B2 = 1 #input-2, case 2
#Calculations
#case-1
d1=(b1 and (not B1))+(B1 and (not b1)) #difference
r1=(b1 and (not B1)) #borrow
#case-2
d2=(b2 and (not B2))+(B2 and (not b2))
r2=(b2 and (not B2))
#Result
print("difference case-1:")
print("d1 = %d"%d1)
print("borrow case-1:")
print("r1 = %.f\n"%r1)
print("difference case-2:")
print("d2 = %.f"%d2)
print("borrow case-2:")
print("r2 = %.f\n"%r2)
# find final output
import math
#Variable declaration
b=1 #input-1
B=0 #input-2
#Calculations
y=(not((b or B)) or (b and B))
#Result
print("final output:")
print("y = %d"%y)
# decoder output
import math
#VAriable declaration
#initial conditions
b = 0 # input
Bi = 0 # initial value
Bf = 1 # final value
#initial state of outputs
y1i=not(b or Bi)
y2i=not(b or (not Bi))
y3i=not(Bi or (not(b)))
y4i=not((not(Bi)) or (not(b)))
#final state of outputs
y1f=not(b or Bf)
y2f=not(Bf or (not(b)))
y3f=not(b or (not(Bf)))
y4f=not((not(Bf)) or (not(b)))
print("first: ")
print("y1 = %.f "%y1i)
print("y2 = %.f "%y2i)
print("y3 = %.f "%y3i)
print("y4 = %.f\n"%y4i)
print("next: ")
print("y1 = %.f "%y1f)
print("y2 = %.f "%y2f)
print("y3 = %.f "%y3f)
print("y4 = %.f"%y4f)
# decoder output
import math
#VAriable declaration
#initial conditions
b = 1 # input
Bi = 0 # initial value
Bf = 1 # final value
#initial state of outputs
y1i=not(b or Bi)
y2i=not(Bi or (not(b)))
y3i=not(b or (not Bi))
y4i=not((not(Bi)) or (not(b)))
#final state of outputs
y1f=not(b or Bf)
y2f=not(b or (not(Bf)))
y3f=not(Bf or (not(b)))
y4f=not((not(Bf)) or (not(b)))
print("first: ")
print("y1=%.f "%y1i)
print("y2=%.f "%y2i)
print("y3=%.f "%y3i)
print("y4=%.f\n"%y4i)
print("next: ")
print("y1=%.f "%y1f)
print("y2=%.f "%y2f)
print("y3=%.f "%y3f)
print("y4=%.f"%y4f)
# convert 8421 to 2421 code
import math
#Variable declaration
#if A8,B8,C8,D8 is the binary in 8421 code, for 12 this would be 1100(DCBA)
#in 8421-code
A8 = 0
B8 = 0
C8 = 1
D8 = 1
#Calculations
#in 2421-code
D2 = D8
C2 =(C8 or D8)
B2 =(B8 or D8)
A2=A8
#Result
print("2421-code for 12(1100) is:")
print("%d%d%d%d "%(D2,C2,B2,A2))
# Xcess 3 code
import math
#Variable declaration
add = "0011" #binary-3 to be added
x = "0010" #binary-2
y = "0100" #binary-4
z = "0111" #binary-7
#Calculations
x = int(x,2)
add = int(add,2)
XS31=x+add
XS31=bin(XS31)[2:]
y=int(y,2)
XS32=y+add
XS32=bin(XS32)[2:]
z=int(z,2)
XS33=z+add
XS33=bin(XS33)[2:]
#Result
print("XS-3 for 2: %s"%XS31)
print("XS-3 for 4: %s"%XS32)
print("XS-3 for 7: %s"%XS33)
# 8 to 1 MUX
# one can see from relations of AND gate outputs in terms of address bits and input bit
# that there are possibilities depending on which input is low
# if I7=0, by making all address bits s1,s2,s3 as 1 one can have the conditions satisfied
#Result
print("This requires all the outputs from AND gates should be low")
# truth table of RS flip flop
import math
#Variable declaration
#enter binary 1-bit values only
S = input("Enter value of S\n")
R = input("Enter value of R\n")
Qn = input("Enter previous value of Q\n")
En = input("Enter enable value\n")
print("RS flip-flop truth table:")
if (En==0):
op=Qn
print("op = %d"%op)
elif(( S==0) and (R==0)):
op=Qn
print("op = %d"%op)
elif ((S==0) and (R==1)):
op=0
print("op = %d"%op)
elif((S==1) and (R==0)):
op=1
print("op = %d"%op)
elif ((S==1) and (R==1)):
print("output not determinable")
print("the relations are:")
print("Qn=(R+Qn*)*") #Q* = not(Q)
print("Qn*=(S+Qn)*")
# JK fiip flop
# Q=(Q*+Q.K)* and Q*=(Q+Q*.J)*
# with J=K=0 Q=Q and Q*=Q*
# Q* is not(Q)
#Result
print("operational equations:\n")
print("Q=(Q*+Q.K)* and Q*=(Q+Q*.J)*\n")
print("holds good where Q and Q* should be given appropriate values")
# 3 bit binary counter
print("It remains positive from the falling edge of pulse-2, then falling edge of 4th, 6th and 8th pulses and so on....")
# 6 modulo counter
print("All modulo counters are basically scalars.A 6-modulo counter is a 6-scaler")
print("so that after 6-input pulses the content of counter becomes 000(reset)")
# 3 bit 5 modulo counter
print("Normal count would be 2^3=8, while 5-modulo counter would limit it to 5, so that illegitimate states are 8-5=3")