Facebook
From Sexy Tamarin, 2 Months ago, written in C++.
Embed
Download Paste or View Raw
Hits: 79
  1. #include <iostream>
  2. #include <climits>
  3. using namespace std;
  4.  
  5. string DecToBin(int);
  6.  
  7. int main(int argc, const char * argv[]) {
  8.     unsigned short max = USHRT_MAX;
  9.     unsigned short input;
  10.     cout<<"Podaj liczbe: ";
  11.     cin>>input;
  12.     string bin = DecToBin(input);
  13.     int diff = 16-bin.length();
  14.     string wholeBin="";
  15.     for (int i = 0; i<diff; i++){
  16.         wholeBin+="0";
  17.     }
  18.     wholeBin+=bin;
  19.     cout<<"Binarnie:\t"<<wholeBin<<endl;
  20.  
  21.     string result;
  22.     for (int i = wholeBin.length()-1; i>=0; i--){
  23.         result+=wholeBin[i];
  24.     }
  25.     cout<<"Reverse:\t"<<result;
  26. }
  27.  
  28. string DecToBin(int number)
  29. {
  30.     if ( number == 0 ) return "0";
  31.     if ( number == 1 ) return "1";
  32.  
  33.     if ( number % 2 == 0 )
  34.         return DecToBin(number / 2) + "0";
  35.     else
  36.         return DecToBin(number / 2) + "1";
  37. }