XOR bitset when 2D bitset is stored as 1D

后端 未结 1 934
轻奢々
轻奢々 2021-01-16 13:13

To answer How to store binary data when you only care about speed?, I am trying to write some to do comparisons, so I want to use std::bitset. However, for fair

1条回答
  •  无人及你
    2021-01-16 13:25

    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, 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> instead. Also, std::bitset::set() sets all bits to 1.

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main()
    {
        const int N = 1000000;
        const int D = 100;
    
        std::vector hamming_dist(N);
    
        std::bitset q;
        q.set();
    
        std::vector> 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;
    }
    

    0 讨论(0)
提交回复
热议问题