You are on page 1of 27

NAME – PRASHANT

COLLEGE ROLL NO- 20MTS5742


PAPER NAME – COMPUTER SYSTEM
ACHITECTURE
COURSE NAME- B.SC (PROGRAM)
MATHEMATICAL SCIENCE
SEMESTER – 4th
#1) Write a program to convert an unsigned number in one radix 'A' to the equivalent
#number in another radix B', where A and B can be any positive integer.

# Program to convert a decimal number in Radix 'R' and given radix to decimal
def val(c):
if c>='0' and c<='9':
return ord(c)-ord('0')
else:
return ord(c)-ord('A')+10;
def toDeci(str,base):
llen=len(str)
power=1
num=0
for i in range(llen-1,-1,-1):
if val(str[i])>=base:
print('Invalid Number')
return -1
num += val(str[i])*power
power=power*base
return num
def reVal(num):
if(num>=0 and num<=9):
return chr(num+ord('0'))
else:
return chr(num-10+ord('A'))
def fromDeci(base,inputNum):
res=''
while(inputNum>0):
res+=reVal(inputNum%base)
inputNum=int(inputNum/base)
res=res[::-1]
return res
def main():
print("\n--------------------MENU------------------\n"
"1.Binary_to_Decimal [OR] Decimal_to_binary\n"
"2.Binary_to_Octal [OR] Octal_to_Binary\n"
"3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary\n"
"4.Decimal_to_Octal [OR] Octal_to_Decimal\n"
"5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal\n"
"6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal\n"
"7.Exit")
Choice=int(input("Enter your choice: "))
if Choice==1:
print("--------Binary Decimal Conversion--------")
A1=int(input("Enter value of Radix'A': "))
B1=int(input("Enter value of Radix'B': "))
if (A1==2 and B1==10):
strr=input("Enter any binary number: ")
print(strr,'in decimal is',toDeci(strr,2))
main()
else:
strr=int(input("Enter any decimal number: "))
print(strr,"in binary is",fromDeci(2,strr))
main()
elif Choice==2:
print("--------Binary Octal Conversion---------")
A2=int(input("Enter value of Radix'A': "))
B2=int(input("Enter value of Radix'B': "))
if (A2==2 and B2==8):
strr=input("Enter any binary number: ")
newstrr=toDeci(strr,2)
print(strr,"in octal is",fromDeci(8,newstrr))
main()
else:
strr=input("Enter any octal number: ")
newstrr=toDeci(strr,8)
print(strr,"in binary is",fromDeci(2,newstrr))
main()
elif Choice==3:
print("---------Binary Hexadecimal Conversion--------")
A3=int(input("Enter value of Radix'A': "))
B3=int(input("Enter value of Radix'B': "))
if (A3==2 and B3==16):
strr=input("Enter any binary number: ")
newstrr=toDeci(strr,2)
print(strr,"in hexadecimal is",fromDeci(16,newstrr))
main()
else:
strr=input("Enter any hexadecimal number: ")
newstrr=toDeci(strr,16)
print(strr,"in binary is",fromDeci(2,newstrr))
main()
elif Choice==4:
print("---------Decimal Octal Conversion--------")
A4=int(input("Enter value of Radix'A': "))
B4=int(input("Enter value of Radix'B': "))
if (A4==10 and B4==8):
strr=input("Enter any decimal number: ")
newstrr=toDeci(strr,10)
print(strr,"in octal is",fromDeci(8,newstrr))
main()
else:
strr=input("Enter any octal number: ")
newstrr=toDeci(strr,8)
print(strr,"in decimal is",fromDeci(10,newstrr))
main()
elif Choice==5:
print("---------Decimal Hexadecimal Conversion--------")
A5=int(input("Enter value of Radix'A': "))
B5=int(input("Enter value of Radix'B': "))
if (A5==10 and B5==16):
strr=input("Enter any decimal number: ")
newstrr=toDeci(strr,10)
print(strr,"in hexadecimal is",fromDeci(16,newstrr))
main()
else:
strr=input("Enter any hexadecimal number: ")
newstrr=toDeci(strr,16)
print(strr,"in decimal is",fromDeci(10,newstrr))
main()
elif Choice==6:
print("---------Octal Hexadecimal Conversion--------")
A6=int(input("Enter value of Radix'A': "))
B6=int(input("Enter value of Radix'B': "))
if (A6==8 and B6==16):
strr=input("Enter any octal number: ")
newstrr=toDeci(strr,8)
print(strr,"in hexadecimal is",fromDeci(16,newstrr))
main()
else:
strr=input("Enter any hexadecimal number: ")
newstrr=toDeci(strr,16)
print(strr,"in octal is",fromDeci(8,newstrr))
main()
elif Choice==7:
print("Ending the program")
exit()
else:
print("\n Invalid Input.")
main()
main()

OUTPUT –
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 1
--------Binary Decimal Conversion--------
Enter value of Radix'A': 2
Enter value of Radix'B': 10
Enter any binary number: 11011011
11011011 in decimal is 219
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 1
--------Binary Decimal Conversion--------
Enter value of Radix'A': 10
Enter value of Radix'B': 2
Enter any decimal number: 100
100 in binary is 1100100
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 2
--------Binary Octal Conversion---------
Enter value of Radix'A': 2
Enter value of Radix'B': 8
Enter any binary number: 110
110 in octal is 6
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 2
--------Binary Octal Conversion---------
Enter value of Radix'A': 8
Enter value of Radix'B': 2
Enter any octal number: 233
233 in binary is 10011011
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 3
---------Binary Hexadecimal Conversion--------
Enter value of Radix'A': 2
Enter value of Radix'B': 16
Enter any binary number: 11111011101110010
11111011101110010 in hexadecimal is 1F772
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 3
---------Binary Hexadecimal Conversion--------
Enter value of Radix'A': 16
Enter value of Radix'B': 2
Enter any hexadecimal number: 3AB2
3AB2 in binary is 11101010110010
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 4
---------Decimal Octal Conversion--------
Enter value of Radix'A': 10
Enter value of Radix'B': 8
Enter any decimal number: 33
33 in octal is 41
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 4
---------Decimal Octal Conversion--------
Enter value of Radix'A': 8
Enter value of Radix'B': 10
Enter any octal number: 173
173 in decimal is 123
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 5
---------Decimal Hexadecimal Conversion--------
Enter value of Radix'A': 10
Enter value of Radix'B': 16
Enter any decimal number: 317547
317547 in hexadecimal is 4D86B
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 5
---------Decimal Hexadecimal Conversion--------
Enter value of Radix'A': 16
Enter value of Radix'B': 10
Enter any hexadecimal number: 1C2
1C2 in decimal is 450
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 6
---------Octal Hexadecimal Conversion--------
Enter value of Radix'A': 8
Enter value of Radix'B': 16
Enter any octal number: 123
123 in hexadecimal is 53
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 6
---------Octal Hexadecimal Conversion--------
Enter value of Radix'A': 16
Enter value of Radix'B': 8
Enter any hexadecimal number: AC3
AC3 in octal is 5303
====================MENU====================
1.Binary_to_Decimal [OR] Decimal_to_binary
2.Binary_to_Octal [OR] Octal_to_Binary
3.Binary_to_Hexadecimal [OR] Hexadecimal_to_Binary
4.Decimal_to_Octal [OR] Octal_to_Decimal
5.Decimal_to_Hexadecimal [OR] Hexadecimal_to_Decimal
6.Octal_to_Hexadecimal [OR] Hexadecimal_to_Octal
7.Exit
Enter your choice: 7
# Q2Write a program that will prompt for the input of two integer values. Then using the Bitwise shift operators
show the result of

#a. Left shifting the first number by the second

#b. Right shifting the first number by the second

def main():

print("\n--------OPERATOR MENU---------\n"

"1. Left Shift.\n"

"2. Right Shift.\n"

"3. Exit")

ch=int(input("Enter your Choice: "))

if (ch==1):

x=input("Value 1: ")

y=input("Value 2: ")

print("Result: ",int(x)<<int (y))

main()

elif(ch==2):

x=input("Value 1: ")

y=input("Value 2: ")

print("Result: ",int(x) >>int(y))

main()
elif (ch==3):

print("Bye")

exit()

else:

print("Wrong Input. Please try again.")

main()

if __name__=="__main__":

main()

OUTPUT –

--------OPERATOR MENU---------
1. Left Shift.
2. Right Shift.
3. Exit
Enter your Choice: 1
Value 1: 2
Value 2: 1
Result: 4

--------OPERATOR MENU---------
1. Left Shift.
2. Right Shift.
3. Exit
Enter your Choice: 2
Value 1: 4
Value 2: 1
Result: 2

--------OPERATOR MENU---------
1. Left Shift.
2. Right Shift.
3. Exit
Enter your Choice: 3
Bye
# Q3 Write a program that will prompt for the input of two integer values Then using the logical operators show the
result of

#a. Exclusive OR of the first number by the second bitwise

#b. OR of the first number by the second bitwise

#c. AND of the first number by the second bitwise

def main ():

print("\n-----OPERATOR MENU------\n"

"1. Bitwise XOR operator.\n"

"2. Bitwise AND operator.\n"

"3. Bitwise OR operator.\n"

"4. To Exit. \n")

ch=int(input("Enter your choice: "))

if(ch==1):

x=input("value 1: ")

y=input("value 2: ")

print("Result: ", int(x)^ int(y))

main()

elif (ch==2):

x=input("value 1: ")

y=input("value 2: ")

print("Result: ", int(x) & int(y))


main()

elif(ch==3):

x=input("Value 1: ")

y=input("Value 2: ")

print("Result: ",int(x) | int(y))

main()

elif (ch==4):

print("Bye")

exit()

else:

print("wrong Input. Please try again.")

main()

if __name__== "__main__":

main()

OUTPUT –

-----OPERATOR MENU------
1. Bitwise XOR operator.
2. Bitwise AND operator.
3. Bitwise OR operator.
4. To Exit.

Enter your choice: 1


value 1: 2
value 2: 3
Result: 1

-----OPERATOR MENU------
1. Bitwise XOR operator.
2. Bitwise AND operator.
3. Bitwise OR operator.
4. To Exit.

Enter your choice: 2


value 1: 4
value 2: 5
Result: 4

-----OPERATOR MENU------
1. Bitwise XOR operator.
2. Bitwise AND operator.
3. Bitwise OR operator.
4. To Exit.

Enter your choice: 4


Bye
#Q4Write a program that will prompt for the input of a binary value and print.

#a. One's complement b. Two's complement

#1's compliment

def one_comp(l):

p=len(l)

r=""

for i in range(p):

if l[i]=='1':

r+='0'

else:

r+='1'

return r

def two_comp(A):

C=0

for i in range(len(A)):

if A[i]=="1":

C=i

else:

continue

D=A[:C]

E=A[C:]

ND=one_comp(D)

P=ND+E

print(P)

def menu():

print("\n Press 1 for 1's compliment"

"\n Press 2 for 2's compliment"


"\n Press 3 to exit")

x=int(input("Enter your choice \n"))

if x==1:

l=int(input("Enter binary number"))

print(one_comp(str(l)))

menu()

elif x==2:

A=int(input("Enter binary number"))

two_comp(str(A))

menu()

elif x==3 :

exit()

else:

print("INVALID CHOICE")

menu()

menu()

OUTPUT –

Press 1 for 1's compliment


Press 2 for 2's compliment
Press 3 to exit
Enter your choice
1
Enter binary number1001011
0110100

Press 1 for 1's compliment


Press 2 for 2's compliment
Press 3 to exit
Enter your choice
2
Enter binary number101010111
010101001
Press 1 for 1's compliment
Press 2 for 2's compliment
Press 3 to exit
Enter your choice
3
#Q5 Writen program to print the values of a 5 bit binary up-down counter User should be able to specify the up or
down nature of the counter

def increment(counter):

bits = list(counter[::-1]) # Reverse the input and put in a list

for i, bit in enumerate(bits):# Iterate over bits and i is index start from 0

if bit == "0":

bits[i] = "1"

break

else:

bits[i] = "0"

return ''.join(str(x) for x in bits[::-1])

def decrement(counter):

bits = list(counter[::-1]) # Reverse the input and put in a list

for i, bit in enumerate(bits):# Iterate over bits and i is index start from 0

if bit == "1":

bits[i] = "0"

break

else:

bits[i] = "1"

return ''.join(str(x) for x in bits[::-1])

def main():

print("\n--------MENU----------\n"

"1.UP counter.\n"

"2.DOWN counter.\n"

"3.Exit.\n")

choice=int(input("Enter your choice(1/2/3): "))

if choice==1:

bits=input("Enter 5 bit value: ")

counter=list(bits)

counter=''.join(str(x) for x in counter)

counter=increment(counter)
print(counter)

main()

elif choice==2:

bits=input("Enter 5 bit value: ")

counter=list(bits)

counter=''.join(str(x) for x in counter)

counter=decrement(counter)

print(counter)

main()

elif choice==3:

print("Ending the program")

exit()

else:

print("\n Invalid Input.")

main()

main()

OUTPUT –

--------MENU----------
1.UP counter.
2.DOWN counter.
3.Exit.

Enter your choice(1/2/3): 1


Enter 5 bit value: 10101
10110

--------MENU----------
1.UP counter.
2.DOWN counter.
3.Exit.

Enter your choice(1/2/3): 3


Ending the program
#Q6 Write a program to implement bi-wse addition of two numbers.
def addbin(a,b):
result=""
maxz=max(len(a),len(b))
a=a.zfill(maxz)
b=b.zfill(maxz)
carry=0
for i in range(maxz-1,-1,-1):
x=int(a[i])+int(b[i])+carry
if x==1 or x==0:
result+=str(x)
carry=0
elif x==2 :
result+="0"
carry=1
else :
result+="1"
carry=1
result=result[::-1]
if carry !=0 :result=str(carry)+result
print("Addition of the entered binary number is :",result)

def validatebin(m):
for i in m:
i=int(i)
if(i==0 or i==1):
continue
else:
print("\nError!! Please Enter a valid Binary base number.\n"
"\n--------Restarting the Program--------")
main()

def main():

print("\n---------------OPERATOR MENU---------------\n"
"1 Addition.\n"
"2. Exit.\n")
ch=int(input("Enter your Choice: "))
if(ch==1):
print()
print("Addition of 2 binary numbers")
x=input("1st Number : ")
validatebin(x)
y=input("2nd Number : ")
validatebin(y)
addbin(x,y)
main()
elif(ch==2):
exit()
else:
print("Wrong Input. Please try again.")
main()
main()
OUTPUT –

---------------OPERATOR MENU---------------
1 Addition.
2. Exit.

Enter your Choice: 1

Addition of 2 binary numbers


1st Number : 10011
2nd Number : 100111
Addition of the entered binary number is : 111010

---------------OPERATOR MENU---------------
1 Addition.
2. Exit.

Enter your Choice: 2


#Q7 Write a program to implement bit-wise Subtraction of two numbers using 2's complement.

def one_comp(l):

p=len(l)

r=""

for i in range(p):

if l[i]=='1':

r+='0'

else:

r+='1'

return r

def two_comp(A):

C=0

for i in range(len(A)):

if A[i]=="1":

C=i

else:

continue

D=A[:C]

E=A[C:]

ND=one_comp(D)

P=ND+E

print(P)

return P

def addbin(a,b):

result=""

maxz=max(len(a),len(b))

a=a.zfill(maxz)

b=b.zfill(maxz)

carry=0

for i in range(maxz-1,-1,-1):

x=int(a[i])+int(b[i])+carry
if x==1 or x==0:

result+=str(x)

carry=0

elif x==2 :

result+="0"

carry=1

else :

result+="1"

carry=1

result=result[::-1]

if carry !=0 :

result=str(carry)+result

print("It is a positive number :",result)

else:

print("It is a negative number :",two_comp(result))

def validatebin(m):

for i in m:

i=int(i)

if(i==0 or i==1):

continue

else:

print("\nError!! Please Enter a valid Binary base number.\n"

"\n--------Restarting the Program--------")

main()

def main():

print("\n---------------OPERATOR MENU---------------\n"

"1 Subtraction.\n"

"2. Exit.\n")

ch=int(input("Enter your Choice: "))

if(ch==1):
print()

print("subtraction of 2 binary numbers")

x=input("1st Number : ")

validatebin(x)

y=input("Enter number to be subtracted: ")

a=two_comp(y)

validatebin(y)

addbin(x,a)

main()

elif(ch==2):

exit()

else:

print("Wrong Input. Please try again.")

main()

main()

OUTPUT –

---------------OPERATOR MENU---------------
1 Subtraction.
2. Exit.

Enter your Choice: 1

subtraction of 2 binary numbers


1st Number : 100111
Enter number to be subtracted: 10101
01011
001110
It is a negative number : 001110

---------------OPERATOR MENU---------------
1 Subtraction.
2. Exit.

Enter your Choice: 2


#Q8 Write a program to implement Selective Set logical operation using bit wise operators.(WORKING AS OR)

def conv(a,b):

temp=list(a)

for i in range(len(a)):

if b[i]=="1":

temp[i]="1"

else:

pass

a="".join(temp)

print("New number is:",a)

main()

def zfill(a,b):

maxz=max(len(a),len(b))

a=a.zfill(maxz)

b=b.zfill(maxz)

conv(a,b)

def validatebin(m):

for i in m:

if(i=='0' or i=='1'):

continue

else:

print("\nPlease Enter a valid Binary base number.\n")

main()

def main():

print("\n------------------------------\n"

"1. Selective Set.\n"

"2. To Exit.\n")

ch=int(input("Enter your Choice: "))

if(ch==1):

a=input("Enter your number : ")

validatebin(a)
b=input("Enter your logic operand: ")

validatebin(b)

zfill(a,b)

elif(ch==2):

print("\nEnding the Program. Have a Nice Day!!")

exit()

else:

print("\nWrong Input. Please try again.")

main()

main()

OUTPUT –

------------------------------
1. Selective Set.
2. To Exit.

Enter your Choice: 1


Enter your number : 101011
Enter your logic operand: 10101
New number is: 111111

------------------------------
1. Selective Set.
2. To Exit.

Enter your Choice: 2

Ending the Program. Have a Nice Day!!


#Q9 Write a program to implement Selective Complement logical operation using bit-wise operators(WORKING AS
XOR)

def conv(a,b):

temp=list(a)

for i in range(len(a)):

if b[i]=="1":

temp[i]=flip(temp[i])

else:

pass

a="".join(temp)

print("New number is:",a)

main()

def flip(c):

if(c=='0'):

return '1'

else:

return '0'

def zfill(a,b):

maxz=max(len(a),len(b))

a=a.zfill(maxz)

b=b.zfill(maxz)

conv(a,b)

def validatebin(m):

for i in m:

if(i=='0' or i=='1'):

continue

else:

print("\nPlease Enter a valid Binary base number.\n")

main()

def main():

print("\n------------------------------\n"
"1. Selective Complement.\n"

"2. To Exit.\n")

ch=int(input("Enter your Choice: "))

if(ch==1):

a=input("Enter your number : ")

validatebin(a)

b=input("Enter your logic operand: ")

validatebin(b)

zfill(a,b)

elif(ch==2):

print("\nEnding the Program. Have a Nice Day!!")

exit()

else:

print("\nWrong Input. Please try again.")

main()

main()

OUTPUT –

------------------------------
1. Selective Complement.
2. To Exit.

Enter your Choice: 1


Enter your number : 1001111
Enter your logic operand: 10101
New number is: 1011010

------------------------------
1. Selective Complement.
2. To Exit.

Enter your Choice: 2

Ending the Program. Have a Nice Day!!


#Q10 Write a program to implement Selective Clear logical operation using bit wise operators.(WORKING
AS A AND B')

def conv(a,b):
temp=list(a)
for i in range(len(a)):
if b[i]=="1":
temp[i]="0"
else:
pass
a="".join(temp)
print("New number is:",a)
main()

def zfill(a,b):
maxz=max(len(a),len(b))
a=a.zfill(maxz)
b=b.zfill(maxz)
conv(a,b)

def validatebin(m):
for i in m:
if(i=='0' or i=='1'):
continue
else:
print("\nPlease Enter a valid Binary base number.\n")
main()
def main():
print("\n------------------------------\n"
"1. Selective Clear.\n"
"2. To Exit.\n")
ch=int(input("Enter your Choice: "))
if(ch==1):
a=input("Enter your number : ")
validatebin(a)
b=input("Enter your logic operand: ")
validatebin(b)
zfill(a,b)
elif(ch==2):
print("\nEnding the Program. Have a Nice Day!!")
exit()
else:
print("\nWrong Input. Please try again.")
main()
main()
OUTPUT –

------------------------------
1. Selective Clear.
2. To Exit.

Enter your Choice: 1


Enter your number : 10101011
Enter your logic operand: 010011
New number is: 10101000

------------------------------
1. Selective Clear.
2. To Exit.

Enter your Choice: 2

Ending the Program. Have a Nice Day!!

You might also like