Advertisement
Guest User

ayush rajthala

a guest
Feb 22nd, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. int a=0,b=0,c=0,com[5]={1,0,0,0,0},s=0;
  4. int anum[5]={0},anumcp[5] ={0},bnum[5]={0};
  5. int acomp[5]={0},bcomp[5]={0},rem[5]={0},quo[5]={0},res[5]={0};
  6. void binary(){
  7. a = fabs(a);
  8. b = fabs(b);
  9. int r, r2, i, temp;
  10. for(i = 0; i < 5; i++){
  11. r = a % 2;
  12. a = a / 2;
  13. r2 = b % 2;
  14. b = b / 2;
  15. anum[i] = r;
  16. anumcp[i] = r;
  17. bnum[i] = r2;
  18. if(r2 == 0){
  19. bcomp[i] = 1;
  20. }
  21. if(r == 0){
  22. acomp[i] =1;
  23. }
  24. }
  25. //part for two's complementing
  26. c = 0;
  27. for( i = 0; i < 5; i++){
  28. res[i] = com[i]+ bcomp[i] + c;
  29. if(res[i]>=2){
  30. c = 1;
  31. }
  32. else
  33. c = 0;
  34. res[i] = res[i]%2;
  35. }
  36. for(i = 4; i>= 0; i--){
  37. bcomp[i] = res[i];
  38. }
  39. }
  40. void add(int num[]){
  41. int i;
  42. c = 0;
  43. for( i = 0; i < 5; i++){
  44. res[i] = rem[i]+ num[i] + c;
  45. if(res[i]>=2){
  46. c = 1;
  47. }
  48. else
  49. c = 0;
  50. res[i] = res[i]%2;
  51. }
  52. for(i = 4; i>= 0; i--){
  53. rem[i] = res[i];
  54. printf("%d",rem[i]);
  55. }
  56. printf(":");
  57. for(i = 4; i>= 0; i--){
  58. printf("%d",anumcp[i]);
  59. }
  60. }
  61. void shl(){//for shift left
  62. int i;
  63. for(i = 4; i > 0 ; i--){//shift the remainder
  64. rem[i] = rem[i-1];
  65. }
  66. rem[0] = anumcp[4];
  67. for(i = 4; i > 0 ; i--){//shift the remtient
  68. anumcp[i] = anumcp[i-1];
  69. }
  70. anumcp[0] = 0;
  71. printf("\nSHIFT LEFT: ");//display together
  72. for(i = 4; i>= 0; i--){
  73. printf("%d",rem[i]);
  74. }
  75. printf(":");
  76. for(i = 4; i>= 0; i--){
  77. printf("%d",anumcp[i]);
  78. }
  79. }
  80. int main(){
  81. int i;
  82. printf("\t\tRESTORING DIVISION ALGORITHM");
  83. printf("\nEnter two numbers to Divide : ");
  84. printf("\nBoth must be less than 16");
  85. //simulating for two numbers each below 16
  86. do{
  87. printf("\nEnter A: ");
  88. scanf("%d",&a);
  89. printf("Enter B: ");
  90. scanf("%d",&b);
  91. }while(a>=16 || b>=16);
  92. printf("\nExpected Quotient = %d", a/b);
  93. printf("\nExpected Remainder = %d", a%b);
  94. if(a*b <0){
  95. s = 1;
  96. }
  97. binary();
  98. printf("\n\nUnsigned Binary Equivalents are: ");
  99. printf("\nA = ");
  100. for(i = 4; i>= 0; i--){
  101. printf("%d",anum[i]);
  102. }
  103. printf("\nB = ");
  104. for(i = 4; i>= 0; i--){
  105. printf("%d",bnum[i]);
  106. }
  107. printf("\nB'+ 1 = ");
  108. for(i = 4; i>= 0; i--){
  109. printf("%d",bcomp[i]);
  110. }
  111. printf("\n\n-->");
  112. //division part
  113. shl();
  114. for(i=0;i<5;i++){
  115. printf("\n-->"); //start with subtraction
  116. printf("\nSUB B: ");
  117. add(bcomp);
  118. if(rem[4]==1){//simply add for restoring
  119. printf("\n-->RESTORE");
  120. printf("\nADD B: ");
  121. anumcp[0] = 0;
  122. add(bnum);
  123. }
  124. else{
  125. anumcp[0] = 1;
  126. }
  127. if(i<4)
  128. shl();
  129. }
  130. printf("\n----------------------------");
  131. printf("\nSign of the result = %d",s);
  132. printf("\nRemainder is = ");
  133. for(i = 4; i>= 0; i--){
  134. printf("%d",rem[i]);
  135. }
  136. printf("\nQuotient is = ");
  137. for(i = 4; i>= 0; i--){
  138. printf("%d",anumcp[i]);
  139.  
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement