Skip to content

Instantly share code, notes, and snippets.

@jweinst1
Created December 24, 2025 00:31
Show Gist options
  • Select an option

  • Save jweinst1/3000150f389672d8ea501b483fe598fe to your computer and use it in GitHub Desktop.

Select an option

Save jweinst1/3000150f389672d8ea501b483fe598fe to your computer and use it in GitHub Desktop.
sorting bit sets of numbers based on hamming distance
#include <array>
#include <cstdint>
#include <cstddef>
#include <cmath>
#include <cstdio>
#include <climits>
#include <vector>
#include <cassert>
#include <random>
#include <chrono>
#include <iostream>
#include <limits>
#include <cassert>
#include <memory>
#include <bitset>
static constexpr size_t testSampleSize = 1024 * 1024;
static constexpr size_t hammingThreshold = 29;
static size_t hamming(uint64_t group, uint64_t num) {
//printf("Hamming is %zu\n", __builtin_popcountll(group ^ num));
return __builtin_popcountll(group ^ num);
}
int main(int argc, char const *argv[])
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint64_t> distrib(
std::numeric_limits<uint64_t>::min(),
std::numeric_limits<uint64_t>::max()
);
std::vector<uint64_t> randData;
std::vector<uint64_t> xorCenters;
xorCenters.push_back(0);
size_t curRand = 0;
for (int i = 0; i < testSampleSize; ++i)
{
randData.push_back(static_cast<uint64_t>(distrib(gen)));
}
size_t insertCount = 0;
for (const auto& num: randData)
{
bool didInsert = false;
for (int i = 0; i < xorCenters.size(); ++i)
{
if (xorCenters[i] == 0) {
xorCenters[i] |= num;
didInsert = true;
++insertCount;
break;
}
if (hamming(xorCenters[i], num) <= hammingThreshold) {
xorCenters[i] |= num;
didInsert = true;
++insertCount;
break;
}
}
if (!didInsert) {
xorCenters.push_back(0);
xorCenters[xorCenters.size() - 1] |= num;
++insertCount;
}
}
std::printf("The centers count is %zu, inserts %zu\n", xorCenters.size(), insertCount);
for (const auto& center: xorCenters)
{
std::cout << std::bitset<64>(center) << " center\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment