Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you please change this code from Python to C language? class IFT_510: def restoringDivision(self,Q,M): Q = bin(Q)[2:] temp = bin(M)[2:] M = bin(M)[2:] lengthq

Can you please change this code from Python to C language?

class IFT_510: def restoringDivision(self,Q,M): Q = bin(Q)[2:] temp = bin(M)[2:] M = bin(M)[2:] lengthq = len(Q) lengthm = len(M) count = max(lengthq,lengthm,8) A = "0"*count if count<=8: M = "0"*(8-lengthm)+M Q = "0"*(8-lengthq)+Q else: if lengthq>lengthm: difference = max(lengthq-lengthm,8-lengthm) M = "0"*difference+M else: difference = max(lengthm-lengthq,8-lengthq) Q = "0"*difference+Q M2 = self.twoComplement(M,count) j = count print("M:", M) print("A","\t\t","Q","\t\t","Operation") print(A,"\t",Q,"\t","Initial values") while j>0: A,Q = self.leftShift(A,Q,count) print(A,"\t",Q,"\t","Shift Left A,Q") A = bin(int(A,2) + int(M2,2))[2:] A = A[-count:] print(A,"\t",Q,"\t","A=A-M") if A[0]=="1": A = bin(int(A,2) + int(M,2))[2:] A = A[-count:] Q = Q[:-1]+"0" print(A,"\t",Q,"\t","A<0,Q0=0,A=A+M") else: Q = Q[:-1]+"1" print(A,"\t",Q,"\t","A>0,Q0=1") j-=1 return (A,Q) def twoComplement(self,M,count): temp = M.replace('0','x').replace('1','0').replace('x','1') i = count-1 M2 = "" while i>-1: if temp[i]=='1': M2 = "0"+M2 else: M2 = "1"+M2 break i-=1 if i<0: return M2 else: return temp[:i]+M2 return temp[:i]+M2 def leftShift(self,A,Q,count): left = (A+Q)[1:]+"0" lengthlsv = int(len(left)/2) A = left[:lengthlsv] Q = left[lengthlsv:] return A,Q

s = IFT_510() print("Enter the dividend:") D = int(input()) print("Enter the divisor:") d = int(input()) Remainder,Q= s.restoringDivision(abs(D),abs(d)) Remainder2, Q2 = s.twoComplement(Remainder, len(Remainder)),s.twoComplement(Q, len(Q)) print("Output in Register A:",Remainder) print("Output in Register Q:",Q)

Remainder1 = int(Remainder,2) Q1 = int(Q,2) ls = [[Q1,Remainder1],[-Q1,Remainder1],[Q1,-Remainder1],[-Q1,-Remainder1]]

for x in ls: if D==x[0]*d+x[1]: quotient = x[0] rem = x[1] break if rem<0: print("Remainder:",Remainder2) else: print("Remainder:",Remainder) if quotient<0: print("Quotient:",Q2) else: print("Quotient:",Q) print("Output:",str(quotient)+"R"+str(rem))

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Experts Guide To Database 2

Authors: Bruce L. Larson

1st Edition

0070232679, 978-0070232679

More Books

Students also viewed these Databases questions