Cut substring from cyclic bit

I have (circular) bitset<N>and would like to get a substring i...i+K-1(where it can happen that i = N - 1; K = 5, therefore, it should turn around and get N-1; 0; 1; 2; 3), as another one bitset<K>(K, of course, known at compile time)

The obvious thing does not work, because it operator &does not allow the use of operands of different sizes (although would this be trivial?)

bitset<N> data = ...;
bitset<K> mask; mask = ~mask;
bitset<K> rotated = in << i | in >> (K - i);
bitset<K> slice = rotated & mask;

The following best does not work with large N

bitset<K> slice( rotated.to_ullong() & mask.to_ullong() );

What if not done bitset<min<N,K>::value> operator &(bitset<N>,bitset<K>)? (and maybe not so terribly inefficient, this approach copies the set 3 times)

+3
source share
1 answer

, . convert to ulong , , ulong. , , , , . , , .

:

template〈size_t D_SZ,size_t S_SZ〉
void CopyBitset(std::bitset〈D_SZ〉 &dest, const std::bitset〈S_SZ〉 &source,size_t idx,
    size_t count,size_t destidx=0)
{
        for(size_t i = 0; i != count;++i){
                dest.set((i + destidx) % D_SZ, source[(i + idx) % S_SZ]);      
        }
}

, . , , .

+2

All Articles