XOR bitset when 2D bitset is stored as 1D

老子叫甜甜 提交于 2019-12-01 13:13:55

The problem is that v[i * D] accesses a single bit. In your conceptual model of a 2D bit array, it accesses the bit at row i and column 0.

So v[i * D] is a bool and q is a std::bitset<D>, and the bitwise logical XOR operator (^) applied to those doesn't make sense.

If v is meant to represent a sequence of binary vectors of size D, you should use a std::vector<std::bitset<D>> instead. Also, std::bitset<N>::set() sets all bits to 1.

#include <vector>
#include <iostream>
#include <random>
#include <cmath>
#include <numeric>
#include <bitset>

int main()
{
    const int N = 1000000;
    const int D = 100;

    std::vector<std::size_t> hamming_dist(N);

    std::bitset<D> q;
    q.set();

    std::vector<std::bitset<D>> v(N);
    for (int i = 0; i < N; ++i)
    {
        v[i].set();
    }

    for (int i = 0; i < N; ++i)
    {
        hamming_dist[i] = (v[i] ^ q).count();
    }

    std::cout << "hamming_distance = " << hamming_dist[0] << "\n";

    return 0;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!