Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- i'm gonna describe the algorithm of the program below:
- First the program takes the input from the user
- Then the program calls the displaySignificand() method
- It first converts the input number to its binary form (using the absolute value of the number in case it's negative)
- If the number takes more than 8 bits to store, the program exits
- Otherwise the binary representation of the number is stored.
- displaySign() method is called to print the sign bit
- if the number is positive, 0 is dispalyed
- if the number is negative,1 is displayed
- dispalyExponent() method is called
- Since the number of bits used to store is equivalent to the exponent , therefore it takes that as an argument
- 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)
- After getting the value of the exponent, it is converted to binary and printed.
- Finaly the significand is printed.
- #include <iostream>
- #include <cmath>
- using namespace std;
- void displaySign(int inputNumber);
- void displayExponent(int exponentShiftCount);
- void displaySignificand(int inputNumber);
- int main()
- {
- int inputNumber;
- //get the input number from the user
- cout<<"Enter the number to convert: ";
- cin>>inputNumber;
- //call the displaySignificand method
- displaySignificand(inputNumber);
- return 0;
- }
- //this method displays the sign bit depending on whether the number is positive or negative
- void displaySign(int inputNumber){
- int sign;
- if(inputNumber>=0)
- sign = 0;
- else
- sign = 1;
- cout<<"| "<< sign <<" | ";
- }
- //this method first converts the input number to its binary from
- //if the number takes more than 8 bits to store, the program exits
- //otherwise it calls the function to print the exponent and finally prints the significand
- void displaySignificand(int inputNumber){
- int significand[8], temp[8]={0,0,0,0,0,0,0,0}, count=0, i;
- //get the absolute value of the input number in case it's negative
- int inputNumberCopy = fabs(inputNumber);
- //convert the absolute number to its binary form
- while(inputNumberCopy>0){
- //count stores the number of bits required to store the number
- //if the number takes more than 8 bits to store, exit the program
- if(count>=8){
- cout<<"Sorry, "<<inputNumber<< " is too large to be stored in our system";
- exit(0);
- }
- //if the program does not exit, this is where the number is converted to its binary equivalent
- temp[count] = inputNumberCopy%2;
- inputNumberCopy=inputNumberCopy/2;
- count++;
- }
- //if the program has not exited, print the signt bit
- displaySign(inputNumber);
- //after printing the sign bit print the exponent bits
- displayExponent(count);
- //finally print the significand bits
- for(i=0; i<count; i++) {
- significand[i] = temp[count-i-1];
- cout<<significand[i];
- }
- for(i=count; i<8; i++) {
- significand[i] = temp[i];
- cout<<significand[i];
- }
- cout<<" | ";
- }
- //this method takes the number of bits required to store the input number
- //number of bits is equivalent to the exponential shift, hence it is converted to binary form here and printed
- void displayExponent(int exponentShiftCount){
- int exponent[5], temp[5]={0,0,0,0,0}, count=0, i;
- //since the exponent is stored in 5 bits it can store numbers from -15 to 15 (2^5).
- //so a positive exponent would have the value of 15+exponent (since it first stores values from-15 to and then the positiv values)
- int exponentShift = 15+exponentShiftCount;
- //convert the exponential shift to binary
- while(exponentShift>0){
- temp[count] = exponentShift%2;
- exponentShift=exponentShift/2;
- count++;
- }
- //print the exponential bits
- for(i=0; i<count; i++) {
- exponent[i] = temp[count-i-1];
- cout<<exponent[i];
- }
- for(i=count; i<5; i++) {
- exponent[i] = temp[i];
- cout<<exponent[i];
- }
- cout<<" | ";
- }
Advertisement
Add Comment
Please, Sign In to add comment