Created
January 30, 2024 09:12
-
-
Save bwedding/67977a3cee512cf63dd23cce4af71751 to your computer and use it in GitHub Desktop.
Bitwise distribution Analysis
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 <random> | |
#include <vector> | |
#include <climits> // for UINT_MAX | |
class BitAccumulator { | |
public: | |
BitAccumulator() : bit_counts(32, 0) {} | |
// Method to accumulate bit counts | |
void accumulate(unsigned int number) { | |
for (size_t i = 0; i < bit_counts.size(); ++i) { | |
if (number & (1u << i)) { | |
++bit_counts[i]; | |
} | |
} | |
} | |
void analyzeDistribution(unsigned long long total_numbers) { | |
auto minmax = std::minmax_element(bit_counts.begin(), bit_counts.end()); | |
std::cout << "Minimum bit set count: " << *minmax.first << std::endl; | |
std::cout << "Maximum bit set count: " << *minmax.second << std::endl; | |
for (size_t i = 0; i < bit_counts.size(); ++i) { | |
double percentage = static_cast<double>(bit_counts[i]) / total_numbers * 100; | |
std::cout << "Bit " << i << ": " << bit_counts[i] << " times set (" << percentage << "%)" << std::endl; | |
} | |
} | |
private: | |
std::vector<unsigned long long> bit_counts; // Accumulate counts for each of the 32 bits | |
}; | |
int main() { | |
BitAccumulator accumulator; | |
std::random_device rd; | |
std::mt19937 gen(rd()); | |
std::uniform_int_distribution<unsigned int> dis(0, UINT_MAX); | |
unsigned long long total_numbers = 10000000; // number of random numbers to generate | |
for (unsigned long long i = 0; i < total_numbers; ++i) { | |
unsigned int number = dis(gen); | |
accumulator.accumulate(number); | |
} | |
accumulator.analyzeDistribution(total_numbers); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment