Reputation: 37
folks. I have a situation like, template argument depends in running time. My function:
string DecToBin(int num)
{
bitset<(num < 256 ? 8 : 32)> bits(num);
return bits.to_string();
}
So, it cannot be compile for sure. What a solution can be except example below? Thanks
string DecToBin(int num)
{
if(num < pow(2, 8))
{
bitset<8> bits(num);
return bits.to_string();
} else if(num < pow(2, 16))
{
bitset<16> bits(num);
return bits.to_string();
} else if(num < pow(2, 32))
{
bitset<32> bits(num);
return bits.to_string();
} else
{
bitset<64> bits(num);
return bits.to_string();
}
}
Upvotes: -2
Views: 87
Reputation: 13144
#include <cstdint>
#include <string>
#include <bitset>
#include <iostream>
#include <iomanip>
std::string DecToBin(std::uint64_t num)
{
std::bitset<64> bits( num );
int num_bits{ 8 };
for (uint64_t test{ 0xff };
~test && test < num;
test = (test << 8) | test, num_bits += 8);
return bits.to_string().substr(64 - num_bits);
}
int main()
{
for (std::uint64_t i{1}; i != 0; i <<= 1)
std::cout << std::setw(64) << DecToBin(i) << '\n';
}
00000001
00000010
00000100
00001000
00010000
00100000
01000000
10000000
0000000100000000
0000001000000000
0000010000000000
0000100000000000
0001000000000000
0010000000000000
0100000000000000
1000000000000000
000000010000000000000000
000000100000000000000000
000001000000000000000000
000010000000000000000000
000100000000000000000000
001000000000000000000000
010000000000000000000000
100000000000000000000000
00000001000000000000000000000000
00000010000000000000000000000000
00000100000000000000000000000000
00001000000000000000000000000000
00010000000000000000000000000000
00100000000000000000000000000000
01000000000000000000000000000000
10000000000000000000000000000000
0000000100000000000000000000000000000000
0000001000000000000000000000000000000000
0000010000000000000000000000000000000000
0000100000000000000000000000000000000000
0001000000000000000000000000000000000000
0010000000000000000000000000000000000000
0100000000000000000000000000000000000000
1000000000000000000000000000000000000000
000000010000000000000000000000000000000000000000
000000100000000000000000000000000000000000000000
000001000000000000000000000000000000000000000000
000010000000000000000000000000000000000000000000
000100000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000
010000000000000000000000000000000000000000000000
100000000000000000000000000000000000000000000000
00000001000000000000000000000000000000000000000000000000
00000010000000000000000000000000000000000000000000000000
00000100000000000000000000000000000000000000000000000000
00001000000000000000000000000000000000000000000000000000
00010000000000000000000000000000000000000000000000000000
00100000000000000000000000000000000000000000000000000000
01000000000000000000000000000000000000000000000000000000
10000000000000000000000000000000000000000000000000000000
0000000100000000000000000000000000000000000000000000000000000000
0000001000000000000000000000000000000000000000000000000000000000
0000010000000000000000000000000000000000000000000000000000000000
0000100000000000000000000000000000000000000000000000000000000000
0001000000000000000000000000000000000000000000000000000000000000
0010000000000000000000000000000000000000000000000000000000000000
0100000000000000000000000000000000000000000000000000000000000000
1000000000000000000000000000000000000000000000000000000000000000
Upvotes: 2