Finding two consecutive 1s in a bit string is less than n times?

I am trying to find out if there are 2 consecutive n bits in a bit string that are n times less than n times.

For example, let's say we had a bit string size of 5 (index 0-4). If indices 1 and 3 were 0, I could return false. But if they were both, then I might need 5 peeks to find my answer.

The bit string should not be 5. For simplicity, let's say, it can be from 3 to 8.

+4
source share
1 answer

The simplest solution could be a bitwise AND source line with the version itself, which was shifted left or right by 1 bit. If the resulting bit string is not zero, then you have at least one 11 :

 test = (src & (src << 1)); 
+14
source

All Articles