Si param_count
est connu au moment de la compilation, vous pouvez utiliser std::bitset
. Voici un exemple:
// Define a bitmap with 'param_count + 7' elements
std::bitset<param_count + 7> b;
// Set the fifth bit, zero is the first bit
b[4] = 1;
// Convert to 'unsigned long', and the casting it to an int.
int a = int(b.to_ulong());
Si param_count
est pas connu au moment de la compilation, vous pouvez utiliser std::vector<bool>
. Voici un autre exemple:
// Define a bitmap with 'param_count + 7' elements
std::vector<bool> b(param_count + 7);
// Set the fifth bit, zero is the first bit
b[4] = 1;
// Convert to 'int'
int a = std::accumulate(b.rbegin(), b.rend(), 0, [](int x, int y) { return (x << 1) + y; });
La conversion de std::vector<bool>
à int
est tiré de this answer.
'std :: vector' est un bitmap. Tout comme 'std :: bitset': http://fr.cppreference.com/w/cpp/utility/bitset –
Jonas
Comment transformer un' std :: vector 'en un entier? –
ruipacheco
Si vous utilisez un 'bitset', vous pouvez utiliser la méthode' to_ulong'. L'implémentation du vecteur peut utiliser un bitet sous le capot mais il n'a pas à le faire. –