Searching Words
Prev: Counting Bits
Next: Rearranging Bits and Bytes
Sections
6-1Find First 0-Byte6-2Find First String of 1-Bits of a Given Length6-3Find Longest String of 1-Bits6-4Find Shortest String of 1-Bits
Problems
-
Code an elaboration of Hsieh’s algorithm that will find both the length and position of the longest string of 1’s in a word . You may use the
nlzfunction. -
Code a function for finding the length and position of the shortest string of 1’s in a word that is at least as long as a given integer .
-
Another way to find the shortest string of 1’s in a word is to successively turn off the rightmost string of 1’s in and observe the change in population count at each step. Code a function for the full RISC that uses this idea and also finds the position of a shortest string of 1’s.
-
For completely random 32-bit words , each bit independently 0 or 1 with probability 0.5, what is the average number of strings of 1’s in ? The answer determines the average execution time of the function of exercise 3, for such input data.
-
Again, for completely random 32-bit words , what is the average length of the shortest contiguous string of 1’s in ? The answer determines the average execution time of function
fminstr1in Figure 6-8 for such input data. Compute this with a Monte Carlo or exhaustive enumeration program. -
Of the binary words of length , for how many is their shortest contained string of 1’s of length 1? That is, how many -bit words begin with
10, or end with01, or contain the sequence010? Find a closed-form solution or a recursion, not an exhaustive enumeration program. -
Similarly, of the binary words of length , for how many is their shortest contained string of 1’s of length 2?