samedi 7 mars 2015

invert n different random bits in int

How could i efficiently invert n bits in unsigned int x? Each bit must be at DIFFERENT randomly picked position. Is there an O(n) solution?



// rnd_engine is kind of std::minstd_rand
template< typename rnd_engine >
unsigned int invert_bits( unsigned int x, unsigned int n, rnd_engine& rndEngine ) {
assert( n <= sizeof( x ) * 4 );
return // your ideas?
}




Aucun commentaire:

Enregistrer un commentaire