Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- string ToBinary(int number) {
- string out;
- for (int bit = 1; bit <= 32; bit*=2) {
- if (number & bit) out = '1' + out;
- else out = '0' + out;
- }
- return out;
- }
- int FromBinary(string in) {
- int out = 0;
- int bit = 1;
- for (int i = in.length()-1; i >= 0; i--) {
- if (in[i] == '1') out = out + bit;
- bit *= 2;
- }
- return out;
- }
- string ReverseStr(string str) {
- string out;
- out.resize(str.length());
- for (int i=str.length()-1;i>=0;i--) {
- out.push_back(str[i]);
- }
- return out;
- }
- int main() {
- for (int i=1;i<10;i++) {
- for (int j=1;j<10;j++) {
- cout<<ToBinary(i+j)<<" VS. "<<ReverseStr(ToBinary(i+j))<<endl;
- if (ToBinary(i*j).compare(ReverseStr(ToBinary(i*j)))==0) {
- cout<<"Binary palindrome: "<<ToBinary(i+j)<<" {DEC: "<<i+j<<"}"<<endl;
- }
- }
- }
- cout<<"Press ENTER to continue..."<<endl;
- cin;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement