Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. string ToBinary(int number) {
  5.      string out;
  6.      for (int bit = 1; bit <= 32; bit*=2)  {
  7.           if (number & bit) out = '1' + out;
  8.           else out = '0' + out;
  9.      }
  10.      return out;
  11. }
  12. int FromBinary(string in) {
  13.      int out = 0;
  14.      int bit = 1;
  15.      for (int i = in.length()-1; i >= 0; i--) {
  16.           if (in[i] == '1') out = out + bit;
  17.           bit *= 2;
  18.      }
  19.      return out;
  20. }
  21. string ReverseStr(string str) {
  22.     string out;
  23.     out.resize(str.length());
  24.     for (int i=str.length()-1;i>=0;i--) {
  25.         out.push_back(str[i]);
  26.         }
  27.     return out;
  28. }
  29.  
  30. int main() {
  31.     for (int i=1;i<10;i++) {
  32.         for (int j=1;j<10;j++) {
  33.             cout<<ToBinary(i+j)<<" VS. "<<ReverseStr(ToBinary(i+j))<<endl;
  34.             if (ToBinary(i*j).compare(ReverseStr(ToBinary(i*j)))==0) {
  35.                 cout<<"Binary palindrome: "<<ToBinary(i+j)<<"    {DEC: "<<i+j<<"}"<<endl;
  36.             }
  37.         }
  38.     }
  39.     cout<<"Press ENTER to continue..."<<endl;
  40.     cin;
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement