fork(2) download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. template <typename T>
  6. T binToDec(const string & bin)
  7. {
  8. T result = 0;
  9. T powerOf2 = T(1);
  10. for (auto i = bin.rbegin(); i != bin.rend(); ++i, powerOf2 <<= 1)
  11. result += (*i - '0') * powerOf2;
  12. return result;
  13.  
  14. }
  15.  
  16. int main()
  17. {
  18. string num[] = {"0", "1", "10", "11", "100", "101", "110", "111", "1000"};
  19.  
  20. for (auto & n : num)
  21. cout << binToDec<unsigned int>(n) << endl;
  22.  
  23. cout << binToDec<unsigned long long>("1111111111111111111111111111111111111111111111111111111111111111") << endl;
  24. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
0
1
2
3
4
5
6
7
8
18446744073709551615