mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Fixed bug in toBin function
This commit is contained in:
@@ -608,7 +608,7 @@ bool isPalindrome(std::string str){
|
||||
template <class T>
|
||||
std::string toBin(T num){
|
||||
//Convert the number to a binary string
|
||||
std::string fullString = std::bitset<sizeof(T)>(num).to_string();
|
||||
std::string fullString = std::bitset<sizeof(T) * 8>(num).to_string();
|
||||
//Remove leading zeros
|
||||
int loc = 0;
|
||||
for(loc = 0;(loc < fullString.size()) && (fullString[loc] == '0');++loc);
|
||||
|
||||
@@ -553,21 +553,29 @@ bool testToBin(){
|
||||
}
|
||||
|
||||
//Test 2
|
||||
uint64_t num2 = 8;
|
||||
correctAnswer = "1000";
|
||||
answer = mee::toBin(num2);
|
||||
num = 0;
|
||||
correctAnswer = "0";
|
||||
answer = mee::toBin(num);
|
||||
if(correctAnswer != answer){
|
||||
return false;
|
||||
}
|
||||
|
||||
//Test 3
|
||||
num = 0;
|
||||
correctAnswer = "0";
|
||||
num = 1000000;
|
||||
correctAnswer = "11110100001001000000";
|
||||
answer = mee::toBin(num);
|
||||
if(correctAnswer != answer){
|
||||
return false;
|
||||
}
|
||||
|
||||
//Test 4
|
||||
uint64_t num2 = 8;
|
||||
correctAnswer = "1000";
|
||||
answer = mee::toBin(num2);
|
||||
if(correctAnswer != answer){
|
||||
return false;
|
||||
}
|
||||
|
||||
//If it hasn't failed a test then return true for passing all the tests
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user