bit shift - Shifting binary string to a canonical form -
i need compare binary strings consider them same if 1 circular shift of other. example, need following same:
10011, 11100, 01110, 11001
i think it'll easier store them in canonical way, , check equivalence. strings compared has same length (might more 100 bits), i'll keep length unchanged, , define canonical form smallest binary number can circular shift. example, 00111
canonical form of binary strings shown above.
my question is, given binary string, how can circular shift smallest binary number without checking possible shifts?
if other representation better, i'd happy receive suggestions.
i'll add, flipping string doesn't matter, canonical form of 010011
001101
(if described above) if flipping allowed, canonical form should 001011
(which prefer). possible solution compute canonization of string , of flipped string , choose smaller.
if helps, i'm working in matlab binary vectors, no need code, explanation of way solve enough, thanks!
find longest sequence of 0s, if > 1 shift until first msb.
oops edge case 00100, if have 2 equal longest sequences of zeros, you'd want possible 1 lsb well.
find longest sequence of 1s, if > 1 shift until last lsb
if there no consecutive 1s or 0s shift until lsb 1
that said it's 5 shifts brute force way know work less effort...
Comments
Post a Comment