Hello, I need to display the difference of two binary numbers using the strtol function. I wrote the code, but it outputs it to me in 10 cc, but it should be in the 2nd. Do not tell me how to fix?

void Diff(string bin1, string bin2) { cout << strtol(bin1.c_str(), NULL, 2) - strtol(bin2.c_str(), NULL, 2) << endl; system("pause"); } 

where bin1 and bin2 binary numbers. At the same time, it is this team that needs to be output, and not to make a translation after subtraction ...

  • In the sense - through cout , without translating into a string? such flags, as I recall, are not provided ... - Harry
  • for binary no - Majestio

1 answer 1

std::setbase from <iomanip> - does not work for a binary system. Almost the only way to output directly like this:

 #include <iostream> #include <bitset> using namespace std; void Diff(string bin1, string bin2) { cout << bitset<8>(strtol(bin1.c_str(), NULL, 2) - strtol(bin2.c_str(), NULL, 2)) << endl; } int main() { Diff("111111","11"); return 0; } 

But at the same time, the initial zeros hang, which depend on the bitset capacity used. If you want to cut off the insignificant zeros on the left - you can look here .