Hi,
I'm having a problem wrapping my head around a problem. Here is the problem:
Develop a program that will input a binary string and then, using bitwise operations, do a two's complement on the string and display it. Use UNSIGNED data types for the bit strings.
So far, I have a class that takes a c-string in the constructor and converts that to an integer using atoi() then I set a temporary variable to the ~ (complement) of the variable and add 1. But I don't really know if I'm even doing it right.
I call it like this:
BinaryString convert("00000010");
And it "works" like this:
BinaryString::BinaryString(char* in)
{
binary=0;
input=in;
doConverttoInt();
}
void BinaryString::doConverttoInt()
{
binary =( unsigned int)atoi(input);
std::cout << "binary is " << binary << std::endl;
performComplement();
}
void BinaryString::performComplement()
{
//input
unsigned int temp;
temp = ~binary;
temp+=1;
std::cout << "complement is " << temp << std::endl;
}
This all just produces garbage, as atoi strips the leading 0's, and temp variable gives me some ridiculously high number.