SHARE
TWEET

Untitled

a guest Mar 21st, 2019 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def shift_left(s):
  2.     s=s[1:]
  3.     s=s+"0"
  4.     return (s)
  5. def complement(s):
  6.     d={'0':'1','1':'0'}
  7.     e=''.join(d[x] for x in s)
  8.     l=len(e)
  9.     sum=bin(int(e,2)+int("1",2))
  10.     sum=sum[2:]
  11.     return(sum.zfill(l))
  12. def addzerotostring(x):
  13.     x=list(x)
  14.     x[-1]="0"
  15.     return("".join(x))
  16. def addonetostring(x):
  17.     x=list(x)
  18.     x[-1]="1"
  19.     return("".join(x))
  20.  
  21.        
  22.  
  23. q=int(input("Enter the dividend :"))
  24. m=int(input("Enter the divisor  :"))
  25. Q=bin(q)
  26. M=bin(m)
  27.  
  28. Q=Q[2:]  #final Q
  29. M=M[2:]  #final M
  30.  
  31. if(len(M)>len(Q)):
  32.     Q=Q.zfill(len(M))
  33. else:
  34.     M=M.zfill(len(Q))
  35. print("Binary Value Of M :",M)
  36. print("Binary Value Of Q :",Q)
  37. l=len(Q)
  38. count=l
  39.  
  40. Mc=complement(M)
  41.  
  42. a="0"
  43. for i in range(0,l-1):
  44.     a=a+"0"    
  45.  
  46. while(count>0):
  47.     s=a+Q
  48.    
  49.     value=shift_left(s)
  50.    
  51.     a=value[0:l]
  52.    
  53.     Q=value[l:]
  54.     a=bin(int(a,2)+int(Mc,2))
  55.     if(len(a[2:])==l+1):
  56.         a=a[3:]
  57.     else:
  58.         a=a[2:]
  59.    
  60.     if(a[0]=="1"):
  61.         a=bin(int(a,2)+int(M,2))
  62.         if(len(a[2:])==l+1):
  63.             a=a[3:]
  64.         else:
  65.             a=a[2:]
  66.        
  67.         Q=addzerotostring(Q)
  68.     else:
  69.        
  70.        
  71.         Q=addonetostring(Q)
  72.  
  73.    
  74.    
  75.     count=count-1
  76.  
  77.  
  78. print("Remainder in Binary Form  :",a)
  79. print("Remainder in Decimal Form :",int(a,2))
  80. print("Quotient in Binary Form   :",Q)
  81. print("Quotient in Decimal Form  :",int(Q,2))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top