fork(3) download
  1. #include <cmath>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. long binary_decimal(string num) /* Function to convert binary to dec */
  7. {
  8. long dec = 0, n = 1, exp = 0;
  9. string bin = num;
  10. if (bin.length() > 1020) {
  11. cout << "Binary Digit too large" << endl;
  12. } else {
  13. for (int i = bin.length() - 1; i > -1; i--) {
  14. n = pow(2, exp++);
  15. if (bin.at(i) == '1')
  16. dec += n;
  17. }
  18. }
  19. return dec;
  20. }
  21.  
  22. int main()
  23. {
  24. std::string str;
  25. while (std::cin >> str) {
  26. std::cout << binary_decimal(str) << '\n';
  27. }
  28. }
  29.  
Success #stdin #stdout 0s 3280KB
stdin
101
01111111111111111111111111111111
11111111111111111111111111111111
1111111111111111111111111111111111
stdout
5
2147483647
-1
-1