0

Hi,

I have been working on a way to reverse bitwise Xand with c++. I have managed to do most of it but can anybody tweak this script I made so it works to its full potential. At the moment this script tries to return the variable x from ((12 + x ) & 1456). I have managed to make this script return a value of x but I wan't a specific possible value of x. That is the highest possible value of x and the lowest possible value of x. These two values could be separated by a bool input in the function to determine the output (highest/lowest) but does anybody know how to do this? My code is as follows and this is a project I have come up with by myself.

```
#include<string.h>
#include<malloc.h>
#include<math.h>
#include<stdlib.h>
#include<iostream>
#include <sstream>
unsigned long int bin2dec(std::string bin)
{
unsigned long int result=0;
for(unsigned int i=0;i<bin.length();i++)
{
//if((bin[i]!='0')&&(bin[i]!='1'))
// return -1;
result=result*2+(bin[i]-'0');
//if(result<=0) return -1;
}
return result;
}
std::string dec2bin (unsigned long int n) {
unsigned int i,j,b[100];
std::string result="";
i=0;
while(n>0)
{
std::stringstream out;
out << (n%2);
result=out.str()+result;
n=n/2;
//i++;
}
/*printf("\n\nBinary Equivalent:");
j=i-1;
for(i=j;j>=0;j--)
printf("%d",b[j]);*/
return result;
}
unsigned long int bin_and_convert(unsigned long int greater_than, unsigned long int right, unsigned long int total_equals) {
std::string lenstr = dec2bin(greater_than);
unsigned int len = lenstr.length();
len--;
std::string subject = dec2bin(total_equals);
std::string subj = subject;
std::string tmp;
subj=subject;
int num,numb;
num=0;
numb=0;
std::string str;
str="";
for (unsigned int m=1;m<=subject.length();m++) {
for (unsigned int i=len;i<subject.length();i++) {
if (subj.at(i)=='0') {
num++;
str+='1';
subj.at(i)='1';
if (((greater_than + bin2dec(subj)) & right)==total_equals) {
return bin2dec(subj);
break;
}
for (unsigned int j=len;j<subj.length();j++) {
if (subj.at(j)=='0') {
subj.at(j)='1';
if (((greater_than + bin2dec(subj)) & right)==total_equals) {
return bin2dec(subj);
break;
}
for (int k=len;k<subj.length();k++) {
if (subj.at(k)=='0') {
subj.at(k)='1';
if (((greater_than + bin2dec(subj)) & right)==total_equals) {
return bin2dec(subj);
break;
}
subj.at(k)='0';
}
}
subj.at(j)='0';
}
}
for (int j=0;j<len;j++) {
if (subj.at(j)=='0') {
subj.at(j)='1';
if (((greater_than + bin2dec(subj)) & right)==total_equals) {
return bin2dec(subj);
break;
}
for (int k=0;k<len;k++) {
if (subj.at(k)=='0') {
subj.at(k)='1';
if (((greater_than + bin2dec(subj)) & right)==total_equals) {
return bin2dec(subj);
break;
}
subj.at(k)='0';
}
}
subj.at(j)='0';
}
}
if (num>=m) {
subj.at(str.length()-1)='0';
}
if (num>=m) {
for (unsigned int jj=0;jj<str.length();jj++) {
if (str.at(jj)=='0') {
str=str.substr(1);
} else {
str=str.substr(1);
break;
}
}
}
} else {
str+='0';
}
}
}
return 0;
}
void main()
{
coda:
system("CLS");
std::cout << std::endl;
std::cout << ((12+20) & 1456) << std::endl;
std::cout << ((12+22) & 1456) << std::endl;
std::cout << bin_and_convert(12,1456,((12+20) & 1456)) << std::endl;
system("PAUSE");
goto coda;
}
```

Thanks.