Advertisement
Guest User

mult

a guest
Feb 22nd, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. int a = 0,b = 0, c = 0, a1 = 0, b1 = 0, com[5] = { 1, 0, 0, 0, 0};
  4. int anum[5] = {0}, anumcp[5] = {0}, bnum[5] = {0};
  5. int acomp[5] = {0}, bcomp[5] = {0}, pro[5] = {0}, res[5] = {0};
  6. void binary(){
  7. a1 = fabs(a);
  8. b1 = fabs(b);
  9. int r, r2, i, temp;
  10. for (i = 0; i < 5; i++){
  11. r = a1 % 2;
  12. a1 = a1 / 2;
  13. r2 = b1 % 2;
  14. b1 = b1 / 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. //in case of negative inputs
  40. if (a < 0){
  41. c = 0;
  42. for (i = 4; i >= 0; i--){
  43. res[i] = 0;
  44. }
  45. for ( i = 0; i < 5; i++){
  46. res[i] = com[i] + acomp[i] + c;
  47. if (res[i] >= 2){
  48. c = 1;
  49. }
  50. else
  51. c = 0;
  52. res[i] = res[i]%2;
  53. }
  54. for (i = 4; i >= 0; i--){
  55. anum[i] = res[i];
  56. anumcp[i] = res[i];
  57. }
  58. }
  59. if(b < 0){
  60. for (i = 0; i < 5; i++){
  61. temp = bnum[i];
  62. bnum[i] = bcomp[i];
  63. bcomp[i] = temp;
  64. }
  65. }
  66. }
  67. void add(int num[]){
  68. int i;
  69. c = 0;
  70. for ( i = 0; i < 5; i++){
  71. res[i] = pro[i] + num[i] + c;
  72. if (res[i] >= 2){
  73. c = 1;
  74. }
  75. else{
  76. c = 0;
  77. }
  78. res[i] = res[i]%2;
  79. }
  80. for (i = 4; i >= 0; i--){
  81. pro[i] = res[i];
  82. printf("%d",pro[i]);
  83. }
  84. printf(":");
  85. for (i = 4; i >= 0; i--){
  86. printf("%d", anumcp[i]);
  87. }
  88. }
  89. void arshift(){//for arithmetic shift right
  90. int temp = pro[4], temp2 = pro[0], i;
  91. for (i = 1; i < 5 ; i++){//shift the MSB of product
  92. pro[i-1] = pro[i];
  93. }
  94. pro[4] = temp;
  95. for (i = 1; i < 5 ; i++){//shift the LSB of product
  96. anumcp[i-1] = anumcp[i];
  97. }
  98. anumcp[4] = temp2;
  99. printf("\nAR-SHIFT: ");//display together
  100. for (i = 4; i >= 0; i--){
  101. printf("%d",pro[i]);
  102. }
  103. printf(":");
  104. for(i = 4; i >= 0; i--){
  105. printf("%d", anumcp[i]);
  106. }
  107. }
  108. void main(){
  109. int i, q = 0;
  110. printf("\t\tBOOTH'S MULTIPLICATION ALGORITHM");
  111. printf("\nEnter two numbers to multiply: ");
  112. printf("\nBoth must be less than 16");
  113. //simulating for two numbers each below 16
  114. do{
  115. printf("\nEnter A: ");
  116. scanf("%d",&a);
  117. printf("Enter B: ");
  118. scanf("%d", &b);
  119. }while(a >=16 || b >=16);
  120. printf("\nExpected product = %d", a * b);
  121. binary();
  122. printf("\n\nBinary Equivalents are: ");
  123. printf("\nA = ");
  124. for (i = 4; i >= 0; i--){
  125. printf("%d", anum[i]);
  126. }
  127. printf("\nB = ");
  128. for (i = 4; i >= 0; i--){
  129. printf("%d", bnum[i]);
  130. }
  131. printf("\nB'+ 1 = ");
  132. for (i = 4; i >= 0; i--){
  133. printf("%d", bcomp[i]);
  134. }
  135. printf("\n\n");
  136. for (i = 0;i < 5; i++){
  137. if (anum[i] == q){//just shift for 00 or 11
  138. printf("\n-->");
  139. arshift();
  140. q = anum[i];
  141. }
  142. else if(anum[i] == 1 && q == 0){//subtract and shift for 10
  143. printf("\n-->");
  144. printf("\nSUB B: ");
  145. add(bcomp);//add two's complement to implement subtraction
  146. arshift();
  147. q = anum[i];
  148. }
  149. else{//add ans shift for 01
  150. printf("\n-->");
  151. printf("\nADD B: ");
  152. add(bnum);
  153. arshift();
  154. q = anum[i];
  155. }
  156. }
  157. printf("\nProduct is = ");
  158. for (i = 4; i >= 0; i--){
  159. printf("%d", pro[i]);
  160. }
  161. for (i = 4; i >= 0; i--){
  162. printf("%d", anumcp[i]);
  163. }
  164. }
  165. /*Program for implementing Restoring Division algorithm.*/
  166. #include <stdio.h>
  167. #include <math.h>
  168. int a=0,b=0,c=0,com[5]={1,0,0,0,0},s=0;
  169. int anum[5]={0},anumcp[5] ={0},bnum[5]={0};
  170. int acomp[5]={0},bcomp[5]={0},rem[5]={0},quo[5]={0},res[5]={0};
  171. void binary(){
  172. a = fabs(a);
  173. b = fabs(b);
  174. int r, r2, i, temp;
  175. for(i = 0; i < 5; i++){
  176. r = a % 2;
  177. a = a / 2;
  178. r2 = b % 2;
  179. b = b / 2;
  180. anum[i] = r;
  181. anumcp[i] = r;
  182. bnum[i] = r2;
  183. if(r2 == 0){
  184. bcomp[i] = 1;
  185. }
  186. if(r == 0){
  187. acomp[i] =1;
  188. }
  189. }
  190. //part for two's complementing
  191. c = 0;
  192. for( i = 0; i < 5; i++){
  193. res[i] = com[i]+ bcomp[i] + c;
  194. if(res[i]>=2){
  195. c = 1;
  196. }
  197. else
  198. c = 0;
  199. res[i] = res[i]%2;
  200. }
  201. for(i = 4; i>= 0; i--){
  202. bcomp[i] = res[i];
  203. }
  204. }
  205. void add(int num[]){
  206. int i;
  207. c = 0;
  208. for( i = 0; i < 5; i++){
  209. res[i] = rem[i]+ num[i] + c;
  210. if(res[i]>=2){
  211. c = 1;
  212. }
  213. else
  214. c = 0;
  215. res[i] = res[i]%2;
  216. }
  217. for(i = 4; i>= 0; i--){
  218. rem[i] = res[i];
  219. printf("%d",rem[i]);
  220. }
  221. printf(":");
  222. for(i = 4; i>= 0; i--){
  223. printf("%d",anumcp[i]);
  224. }
  225. }
  226. void shl(){//for shift left
  227. int i;
  228. for(i = 4; i > 0 ; i--){//shift the remainder
  229. rem[i] = rem[i-1];
  230. }
  231. rem[0] = anumcp[4];
  232. for(i = 4; i > 0 ; i--){//shift the remtient
  233. anumcp[i] = anumcp[i-1];
  234. }
  235. anumcp[0] = 0;
  236. printf("\nSHIFT LEFT: ");//display together
  237. for(i = 4; i>= 0; i--){
  238. printf("%d",rem[i]);
  239. }
  240. printf(":");
  241. for(i = 4; i>= 0; i--){
  242. printf("%d",anumcp[i]);
  243. }
  244. }
  245. int main(){
  246. int i;
  247. printf("\t\tRESTORING DIVISION ALGORITHM");
  248. printf("\nEnter two numbers to Divide : ");
  249. printf("\nBoth must be less than 16");
  250. //simulating for two numbers each below 16
  251. do{
  252. printf("\nEnter A: ");
  253. scanf("%d",&a);
  254. printf("Enter B: ");
  255. scanf("%d",&b);
  256. }while(a>=16 || b>=16);
  257. printf("\nExpected Quotient = %d", a/b);
  258. printf("\nExpected Remainder = %d", a%b);
  259. if(a*b <0){
  260. s = 1;
  261. }
  262. binary();
  263. printf("\n\nUnsigned Binary Equivalents are: ");
  264. printf("\nA = ");
  265. for(i = 4; i>= 0; i--){
  266. printf("%d",anum[i]);
  267. }
  268. printf("\nB = ");
  269. for(i = 4; i>= 0; i--){
  270. printf("%d",bnum[i]);
  271. }
  272. printf("\nB'+ 1 = ");
  273. for(i = 4; i>= 0; i--){
  274. printf("%d",bcomp[i]);
  275. }
  276. printf("\n\n-->");
  277. //division part
  278. shl();
  279. for(i=0;i<5;i++){
  280. printf("\n-->"); //start with subtraction
  281. printf("\nSUB B: ");
  282. add(bcomp);
  283. if(rem[4]==1){//simply add for restoring
  284. printf("\n-->RESTORE");
  285. printf("\nADD B: ");
  286. anumcp[0] = 0;
  287. add(bnum);
  288. }
  289. else{
  290. anumcp[0] = 1;
  291. }
  292. if(i<4)
  293. shl();
  294. }
  295. printf("\n----------------------------");
  296. printf("\nSign of the result = %d",s);
  297. printf("\nRemainder is = ");
  298. for(i = 4; i>= 0; i--){
  299. printf("%d",rem[i]);
  300. }
  301. printf("\nQuotient is = ");
  302. for(i = 4; i>= 0; i--){
  303. printf("%d",anumcp[i]);
  304.  
  305. }
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement