fork download
  1. #include<stdio.h>
  2.  
  3. /*Function declaration*/
  4.  
  5. int fn_find_sign_bit(long numbertocheck);
  6. long fn_convert_to_binary(long numbertoconvert);
  7. int fn_find_binary_length(long numberinbinary);
  8. void fn_binary_to_array(int expectedarraysize,long numberinbinary,int actualbinarylength,int* array);
  9. long fn_generate_exponent_binary(int exponentdecimalnumber);
  10.  
  11. /*Main Function*/
  12.  
  13. int main(){
  14. long firstinput,secondinput;
  15. long originalfirstinput,originalsecondinput;
  16. int firstnumbersign,secondnumbersign;
  17. long firstnumberbinary,secondnumberbinary;
  18. int firstbinarylength,secondbinarylength;
  19. int exponentnumber,exponentshifter;
  20. long finalexponentbinary;
  21. int finalarraysize=23;
  22. int firstsmall=0;
  23. int secsamll=0;
  24. int firbintoarray[finalarraysize];
  25. int secbintoarray[finalarraysize];
  26. int firbtwointoarray[finalarraysize];
  27. int secbtwointoarray[finalarraysize];
  28. int finalresultarray[finalarraysize];
  29. int carry=1;
  30.  
  31. printf("Enter first Input\n");
  32. scanf("%ld",&firstinput);
  33. printf("Enter second Input\n");
  34. scanf("%ld",&secondinput);
  35.  
  36. originalfirstinput=firstinput;
  37. originalsecondinput=secondinput;
  38.  
  39. /*Finding the sign bit*/
  40. firstnumbersign=fn_find_sign_bit(firstinput);
  41. secondnumbersign=fn_find_sign_bit(secondinput);
  42.  
  43. /*converting the sign*/
  44. if(firstinput<0){
  45. firstinput=-1*firstinput;
  46. }
  47. if(secondinput<0){
  48. secondinput=-1*secondinput;
  49. }
  50. if(firstinput<secondinput){
  51. firstsmall=1;
  52. }else{
  53. secsamll=1;
  54. }
  55.  
  56. /*Finding the binary format*/
  57. firstnumberbinary=fn_convert_to_binary(firstinput);
  58. secondnumberbinary=fn_convert_to_binary(secondinput);
  59. printf("First Binary %ld",firstnumberbinary);
  60. printf("\n");
  61. printf("Second Binary %ld",secondnumberbinary);
  62. printf("\n");
  63.  
  64. /*Finding the binary format length*/
  65. firstbinarylength=fn_find_binary_length(firstnumberbinary);
  66. secondbinarylength=fn_find_binary_length(secondnumberbinary);
  67. printf("First Binary length %d",firstbinarylength);
  68. printf("\n");
  69. printf("Second Binary length %d",secondbinarylength);
  70. printf("\n");
  71.  
  72. /*Finding the number for exponent*/
  73. if(firstbinarylength>secondbinarylength){
  74. exponentnumber=(firstbinarylength-1);
  75. exponentshifter=firstbinarylength-secondbinarylength;
  76. }else{
  77. exponentnumber=(secondbinarylength-1);
  78. exponentshifter=secondbinarylength-firstbinarylength;
  79. }
  80. printf("\n----------------------------------------------------------------------------\n");
  81. printf("*** %d shift(s) required for aligning mantissa to make exponents equal *** \n",exponentshifter);
  82. finalexponentbinary=fn_generate_exponent_binary(exponentnumber);
  83. printf("------------------------------------------------------------------------------\n\n");
  84.  
  85. fn_binary_to_array(exponentnumber,firstnumberbinary,(firstbinarylength-1),firbintoarray);
  86. fn_binary_to_array(exponentnumber,secondnumberbinary,(secondbinarylength-1),secbintoarray);
  87. printf("----------------------------------------------------------------------------\n");
  88. printf("32 Bit Floating Format for %ld is %d %ld ",originalfirstinput,firstnumbersign,finalexponentbinary);
  89. for(int icount1=0;icount1<sizeof(firbintoarray)/sizeof(firbintoarray[0]);icount1++){
  90. printf("%d",firbintoarray[icount1]);
  91. }
  92. printf("\n");
  93. printf("----------------------------------------------------------------------------\n");
  94. printf("----------------------------------------------------------------------------\n");
  95. printf("32 Bit Floating Format for %ld is %d %ld ",originalsecondinput,secondnumbersign,finalexponentbinary);
  96. for(int icount1=0;icount1<sizeof(secbintoarray)/sizeof(secbintoarray[0]);icount1++){
  97. printf("%d",secbintoarray[icount1]);
  98. }
  99. printf("\n");
  100. printf("----------------------------------------------------------------------------\n");
  101. if( firstnumbersign != secondnumbersign ){
  102. if(firstsmall==1){
  103. /*Finding ones Complement*/
  104. for(int size=0;size<(sizeof(firbintoarray)/sizeof(firbintoarray[0]));size++){
  105. if(firbintoarray[size]==0){
  106. firbintoarray[size]=1;
  107. }else if (firbintoarray[size]==1){
  108. firbintoarray[size]=0;
  109. }
  110. }
  111. /*Finding twos Complement*/
  112. for(int size=((sizeof(firbintoarray)/sizeof(firbintoarray[0]))-1); size>=0; size--)
  113. {
  114. if(firbintoarray[size]==1 && carry==1)
  115. {
  116. firbtwointoarray[size] = 0;
  117. }
  118. else if(firbintoarray[size]==0 && carry==1)
  119. {
  120. firbtwointoarray[size] = 1;
  121. carry = 0;
  122. }
  123. else
  124. {
  125. firbtwointoarray[size] = firbintoarray[size];
  126. }
  127. }
  128. for(int size=0;size<=(sizeof(secbintoarray)/sizeof(secbintoarray[0]));size++){
  129. secbtwointoarray[size]=secbintoarray[size];
  130. }
  131. }else if (secsamll==1){
  132. /*Finding ones complement*/
  133. for(int size=0;size<(sizeof(secbintoarray)/sizeof(secbintoarray[0]));size++){
  134. if(secbintoarray[size]==0){
  135. secbintoarray[size]=1;
  136. }else if (secbintoarray[size]==1){
  137. secbintoarray[size]=0;
  138. }
  139. }
  140. /*Finding twos Complement*/
  141. for(int size=((sizeof(secbintoarray)/sizeof(secbintoarray[0]))-1); size>=0; size--)
  142. {
  143. if(secbintoarray[size]==1 && carry==1)
  144. {
  145. secbtwointoarray[size] = 0;
  146. }
  147. else if(secbintoarray[size]==0 && carry==1)
  148. {
  149. secbtwointoarray[size] = 1;
  150. carry = 0;
  151. }
  152. else
  153. {
  154. secbtwointoarray[size] = secbintoarray[size];
  155. }
  156. }
  157. for(int size=0;size<=(sizeof(firbintoarray)/sizeof(firbintoarray[0]));size++){
  158. firbtwointoarray[size]=firbintoarray[size];
  159. }
  160. }
  161. printf("\nMantissa after finding two's complement of the small number\n");
  162. printf("----------------------------------------------------------------------------\n");
  163. for(int icount1=0;icount1<sizeof(firbtwointoarray)/sizeof(firbtwointoarray[0]);icount1++){
  164. printf("%d",firbtwointoarray[icount1]);
  165. }
  166. printf("\n");
  167. for(int icount1=0;icount1<sizeof(secbtwointoarray)/sizeof(secbtwointoarray[0]);icount1++){
  168. printf("%d",secbtwointoarray[icount1]);
  169. }
  170. printf("\n");
  171. printf("----------------------------------------------------------------------------\n");
  172. carry=0;
  173. /*Addition of Mantissa*/
  174. for(int size=((sizeof(firbtwointoarray)/sizeof(firbtwointoarray[0]))-1);size>=0;size--){
  175. if(firbtwointoarray[size]==0 && secbtwointoarray[size] ==0 && carry==0){
  176. finalresultarray[size]=0;
  177. carry=0;
  178. }else if(firbtwointoarray[size]==0 && secbtwointoarray[size] ==0 && carry==1){
  179. finalresultarray[size]=1;
  180. carry=0;
  181. }else if(firbtwointoarray[size]==0 && secbtwointoarray[size] ==1 && carry==1){
  182. finalresultarray[size]=0;
  183. carry=1;
  184. }else if(firbtwointoarray[size]==1 && secbtwointoarray[size] ==1 && carry==1){
  185. finalresultarray[size]=1;
  186. carry=1;
  187. }else if(firbtwointoarray[size]==1 && secbtwointoarray[size] ==1 && carry==0){
  188. finalresultarray[size]=0;
  189. carry=1;
  190. }else if(firbtwointoarray[size]==1 && secbtwointoarray[size] ==0 && carry==0){
  191. finalresultarray[size]=1;
  192. carry=0;
  193. }else if(firbtwointoarray[size]==0 && secbtwointoarray[size] ==0 && carry==0){
  194. finalresultarray[size]=0;
  195. carry=0;
  196. }else if(firbtwointoarray[size]==0 && secbtwointoarray[size] ==1 && carry==0){
  197. finalresultarray[size]=1;
  198. carry=0;
  199. }else if(firbtwointoarray[size]==1 && secbtwointoarray[size] ==0 && carry==1){
  200. finalresultarray[size]=0;
  201. carry=1;
  202. }
  203. }
  204. }
  205. else{
  206. printf("\nTwo's complement not required and below Mantissa will be sued for addition\n");
  207. carry=0;
  208. /*Addition of Mantissa*/
  209. for(int size=((sizeof(firbintoarray)/sizeof(firbintoarray[0]))-1);size>=0;size--){
  210. if(firbintoarray[size]==0 && secbintoarray[size] ==0 && carry==0){
  211. finalresultarray[size]=0;
  212. carry=0;
  213. }else if(firbintoarray[size]==0 && secbintoarray[size] ==0 && carry==1){
  214. finalresultarray[size]=1;
  215. carry=0;
  216. }else if(firbintoarray[size]==0 && secbintoarray[size] ==1 && carry==1){
  217. finalresultarray[size]=0;
  218. carry=1;
  219. }else if(firbintoarray[size]==1 && secbintoarray[size] ==1 && carry==1){
  220. finalresultarray[size]=1;
  221. carry=1;
  222. }else if(firbintoarray[size]==1 && secbintoarray[size] ==1 && carry==0){
  223. finalresultarray[size]=0;
  224. carry=1;
  225. }else if(firbintoarray[size]==1 && secbintoarray[size] ==0 && carry==0){
  226. finalresultarray[size]=1;
  227. carry=0;
  228. }else if(firbintoarray[size]==0 && secbintoarray[size] ==0 && carry==0){
  229. finalresultarray[size]=0;
  230. carry=0;
  231. }else if(firbintoarray[size]==0 && secbintoarray[size] ==1 && carry==0){
  232. finalresultarray[size]=1;
  233. carry=0;
  234. }else if(firbintoarray[size]==1 && secbintoarray[size] ==0 && carry==1){
  235. finalresultarray[size]=0;
  236. carry=1;
  237. }
  238. }
  239. }
  240. printf("\nResult after addition\n");
  241. printf("----------------------------------------------------------------------------\n");
  242. if(firstsmall==1){
  243. printf("%d",secondnumbersign);
  244. }else{
  245. printf("%d",firstnumbersign);
  246. }
  247. printf(" %ld ",finalexponentbinary);
  248. for(int size=0;size<sizeof(finalresultarray)/sizeof(finalresultarray[0]);size++){
  249. printf("%d",finalresultarray[size]);
  250. }
  251. printf("\n");
  252. printf("----------------------------------------------------------------------------\n");
  253. return 0;
  254. }
  255.  
  256. /*Function to find the exponent part*/
  257. long fn_generate_exponent_binary(int exponentdecimalnumber){
  258. exponentdecimalnumber=exponentdecimalnumber+127;
  259. return fn_convert_to_binary(exponentdecimalnumber);
  260. }
  261.  
  262. /*Function to convert binary into array*/
  263. void fn_binary_to_array(int expectedarraysize,long numberinbinary,int actualbinarylength,int* array){
  264. int finalarraysize=23;
  265. int binarytoarray[finalarraysize];
  266. for(int icountshifter=0;icountshifter<finalarraysize;icountshifter++){
  267. array[icountshifter]=0;
  268. }
  269.  
  270. for(int icount=0;icount<=(actualbinarylength);icount++){
  271. if(icount<(actualbinarylength)){
  272. array[(expectedarraysize-(1+icount))]=numberinbinary%10;
  273. numberinbinary=numberinbinary/10;
  274. }
  275. }
  276. }
  277.  
  278. /*Function to find out the sign bit*/
  279. int fn_find_sign_bit(long numbertocheck){
  280. if (numbertocheck < 0){
  281. return 1;
  282. }else{
  283. return 0;
  284. }
  285. }
  286.  
  287. /* Function to convert to binary format */
  288. long fn_convert_to_binary(long numbertoconvert) {
  289. long binaryformat=0;
  290. int remainder, positionadjuster=1;
  291. while(numbertoconvert != 0){
  292. remainder = numbertoconvert%2;
  293. binaryformat=binaryformat+(remainder*positionadjuster);
  294. numbertoconvert=numbertoconvert/2;
  295. positionadjuster=positionadjuster*10;
  296. }
  297. return binaryformat;
  298. }
  299.  
  300. /*Function to find the length of binary number*/
  301. int fn_find_binary_length(long numberinbinary){
  302. int numberlength=0;
  303. while(numberinbinary){
  304. numberinbinary=numberinbinary/10;
  305. numberlength=numberlength+1;
  306. }
  307. return numberlength;
  308. }
  309.  
  310.  
Success #stdin #stdout 0s 2176KB
stdin
0
0
stdout
Enter first Input
Enter second Input
First Binary 0
Second Binary 0
First Binary length 0
Second Binary length 0

----------------------------------------------------------------------------
*** 0 shift(s) required for aligning mantissa to make exponents equal *** 
------------------------------------------------------------------------------

----------------------------------------------------------------------------
32 Bit Floating Format  for 0 is 0 1111110 00000000000000000000000
----------------------------------------------------------------------------
----------------------------------------------------------------------------
32 Bit Floating Format  for 0 is 0 1111110 00000000000000000000000
----------------------------------------------------------------------------

Two's complement not required and below Mantissa will be sued for addition

Result after addition
----------------------------------------------------------------------------
0 1111110 00000000000000000000000
----------------------------------------------------------------------------