Skip to content

Instantly share code, notes, and snippets.

@cengiz-io
Last active August 29, 2015 14:11
Show Gist options
  • Save cengiz-io/577c89e211506ee82aac to your computer and use it in GitHub Desktop.
Save cengiz-io/577c89e211506ee82aac to your computer and use it in GitHub Desktop.
#define DEBUG 0
#include <iostream>
#include <sstream>
#include <fstream>
#include <stdlib.h>
#if DEBUG
#include <bitset>
#endif
using namespace std;
const string is_bits_same(int n, int p1, int p2) {
p1--;
p2--;
int p1_mask = 1 << p1;
int p2_mask = 1 << p2;
int p1_masked_n = n & p1_mask;
int p2_masked_n = n & p2_mask;
int b1 = p1_masked_n >> p1;
int b2 = p2_masked_n >> p2;
#if DEBUG
cout << " n: " << bitset<8>(n) << endl;
cout << "*************" << endl;
cout << " p1_mask: " << bitset<8>(p1_mask) << endl;
cout << " p2_mask: " << bitset<8>(p2_mask) << endl;
cout << "*************" << endl;
cout << "p1_masked_n: " << bitset<8>(p1_masked_n) << endl;
cout << "p2_masked_n: " << bitset<8>(p2_masked_n) << endl;
cout << "*************" << endl;
cout << " b1: " << bitset<8>(b1) << endl;
cout << " b2: " << bitset<8>(b2) << endl;
cout << "*************" << endl;
cout << endl;
#endif
return (b1 == b2) ? "true" : "false";
}
int main(int argc, char *argv[]) {
ifstream infile(argv[1]);
string line;
while (getline(infile, line)) {
if (line.length() < 1) continue;
istringstream stream(line);
string token;
int params[3] = {0};
int counter = 0;
while (getline(stream, token, ',')) {
params[counter++] = atoi(token.c_str());
}
if (sizeof(params) / sizeof(params[0]) != 3) continue;
cout << is_bits_same(params[0], params[1], params[2]) << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment