jaredec18

Untitled

Sep 9th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. i'm gonna describe the algorithm of the program below:
  2.  
  3. First the program takes the input from the user
  4. Then the program calls the displaySignificand() method
  5. It first converts the input number to its binary form (using the absolute value of the number in case it's negative)
  6. If the number takes more than 8 bits to store, the program exits
  7. Otherwise the binary representation of the number is stored.
  8. displaySign() method is called to print the sign bit
  9. if the number is positive, 0 is dispalyed
  10. if the number is negative,1 is displayed
  11. dispalyExponent() method is called
  12. Since the number of bits used to store is equivalent to the exponent , therefore it takes that as an argument
  13. it adds that to 15 to get the value of the exponent (since exponent uses 5 bits it can store 31 values i.e -15 to 15) So a positive exponent will appear after -15....0....'position of the positive exponent'. Therefore it has to be added to 15)
  14. After getting the value of the exponent, it is converted to binary and printed.
  15. Finaly the significand is printed.
  16. #include <iostream>
  17. #include <cmath>
  18.  
  19. using namespace std;
  20.  
  21. void displaySign(int inputNumber);
  22. void displayExponent(int exponentShiftCount);
  23. void displaySignificand(int inputNumber);
  24.  
  25.  
  26. int main()
  27. {
  28. int inputNumber;
  29.  
  30. //get the input number from the user
  31. cout<<"Enter the number to convert: ";
  32. cin>>inputNumber;
  33.  
  34. //call the displaySignificand method
  35. displaySignificand(inputNumber);
  36.  
  37. return 0;
  38. }
  39.  
  40. //this method displays the sign bit depending on whether the number is positive or negative
  41. void displaySign(int inputNumber){
  42. int sign;
  43.  
  44. if(inputNumber>=0)
  45. sign = 0;
  46. else
  47. sign = 1;
  48.  
  49. cout<<"| "<< sign <<" | ";
  50.  
  51. }
  52.  
  53. //this method first converts the input number to its binary from
  54. //if the number takes more than 8 bits to store, the program exits
  55. //otherwise it calls the function to print the exponent and finally prints the significand
  56.  
  57. void displaySignificand(int inputNumber){
  58. int significand[8], temp[8]={0,0,0,0,0,0,0,0}, count=0, i;
  59.  
  60. //get the absolute value of the input number in case it's negative
  61. int inputNumberCopy = fabs(inputNumber);
  62.  
  63. //convert the absolute number to its binary form
  64. while(inputNumberCopy>0){
  65. //count stores the number of bits required to store the number
  66. //if the number takes more than 8 bits to store, exit the program
  67. if(count>=8){
  68. cout<<"Sorry, "<<inputNumber<< " is too large to be stored in our system";
  69. exit(0);
  70. }
  71. //if the program does not exit, this is where the number is converted to its binary equivalent
  72. temp[count] = inputNumberCopy%2;
  73. inputNumberCopy=inputNumberCopy/2;
  74. count++;
  75. }
  76.  
  77. //if the program has not exited, print the signt bit
  78. displaySign(inputNumber);
  79.  
  80. //after printing the sign bit print the exponent bits
  81. displayExponent(count);
  82.  
  83.  
  84. //finally print the significand bits
  85. for(i=0; i<count; i++) {
  86. significand[i] = temp[count-i-1];
  87. cout<<significand[i];
  88. }
  89.  
  90. for(i=count; i<8; i++) {
  91. significand[i] = temp[i];
  92. cout<<significand[i];
  93. }
  94.  
  95. cout<<" | ";
  96. }
  97.  
  98.  
  99. //this method takes the number of bits required to store the input number
  100. //number of bits is equivalent to the exponential shift, hence it is converted to binary form here and printed
  101. void displayExponent(int exponentShiftCount){
  102. int exponent[5], temp[5]={0,0,0,0,0}, count=0, i;
  103.  
  104. //since the exponent is stored in 5 bits it can store numbers from -15 to 15 (2^5).
  105. //so a positive exponent would have the value of 15+exponent (since it first stores values from-15 to and then the positiv values)
  106. int exponentShift = 15+exponentShiftCount;
  107.  
  108. //convert the exponential shift to binary
  109. while(exponentShift>0){
  110. temp[count] = exponentShift%2;
  111. exponentShift=exponentShift/2;
  112. count++;
  113. }
  114.  
  115. //print the exponential bits
  116. for(i=0; i<count; i++) {
  117. exponent[i] = temp[count-i-1];
  118. cout<<exponent[i];
  119. }
  120.  
  121. for(i=count; i<5; i++) {
  122. exponent[i] = temp[i];
  123. cout<<exponent[i];
  124. }
  125.  
  126. cout<<" | ";
  127. }
Advertisement
Add Comment
Please, Sign In to add comment