Last active
August 17, 2024 19:42
-
-
Save mudassaralichouhan/1d7cca0403ca49d15eb20eec7f22055c to your computer and use it in GitHub Desktop.
Number System
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
*/ | |
#include "iostream" | |
#include "cmath" | |
class NumberSystem | |
{ | |
public: | |
int binToDac(std::string bin) { | |
int dec = 0; | |
for (int i = 0; i < bin.length(); i++) { | |
dec += int(bin[i] - 48/*'0'*/) * pow(2, bin.size() - i - 1); | |
} | |
return dec; | |
} | |
}; | |
int main(int argc, char *argv[]) { | |
NumberSystem numberSystem; | |
std::string bin = "00010101"; | |
std::cout << "binToDac " << bin << ": " << numberSystem.binToDac(bin) << std::endl; | |
bin = "11111111"; | |
std::cout << "binToDac " << bin << ": " << numberSystem.binToDac(bin) << std::endl; | |
bin = "00000000"; | |
std::cout << "binToDac " << bin << ": " << numberSystem.binToDac(bin) << std::endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "iostream" | |
#include "vector" | |
#define IS_EVEN(i) (i & 1) == 1 | |
using namespace std; | |
class NumberSystem { | |
public: | |
vector<std::string> decToBin(vector<int> dec) | |
{ | |
vector<std::string> bins; | |
for (int i = 0; i < dec.size(); i++) { | |
std::string bin = ""; | |
int decimal = dec[i]; | |
while (decimal > 0) { | |
bin = std::to_string(IS_EVEN(decimal)) + bin; | |
decimal /= 2; | |
} | |
bins.push_back(bin); | |
} | |
return bins; | |
} | |
}; | |
int main() | |
{ | |
vector<int> numbers = {8, 64, 255}; | |
NumberSystem numberSystem; | |
vector<std::string> out = numberSystem.decToBin(numbers); | |
for (const std::string n: out) | |
cout << n << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment