Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- string to_binary(int x) {
- string res = "";
- while(x > 0) {
- int rem = x % 2;
- x /= 2;
- res += (rem + '0');
- }
- reverse(res.begin(), res.end());
- return res;
- }
- int to_decimal(string s) {
- int res = 0;
- int pow_of_two = 1;
- for(int i = (int) s.size() - 1; i >= 0; i--) {
- if(s[i] == '1') {
- res += pow_of_two;
- }
- pow_of_two *= 2;
- }
- return res;
- }
- int main()
- {
- cout << to_binary(786) << endl;
- cout << to_decimal("1100010010") << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement