Solution Found!
a) Show that this algorithm determines the number of 1
Chapter 3, Problem 10E(choose chapter or problem)
Problem 10E
a) Show that this algorithm determines the number of 1 bits in the bit string S:
procedure bit count(S: bit string)
count := 0
while S ≠ 0
count := count + 1
S := S ˄ (S − 1)
return count {count is the number of 1s in S}
Here S − 1 is the bit string obtained by changing the rightmost 1 bit of S to a 0 and all the 0 bi Is to t he right of this to 1s. [Recall that S ˄ (S − 1) is the bitwise AND of S and S − 1.]
b) How many bitwise AND operations are needed to find the number of 1 bits in a string S using the algorithm in pan (a)?
Questions & Answers
QUESTION:
Problem 10E
a) Show that this algorithm determines the number of 1 bits in the bit string S:
procedure bit count(S: bit string)
count := 0
while S ≠ 0
count := count + 1
S := S ˄ (S − 1)
return count {count is the number of 1s in S}
Here S − 1 is the bit string obtained by changing the rightmost 1 bit of S to a 0 and all the 0 bi Is to t he right of this to 1s. [Recall that S ˄ (S − 1) is the bitwise AND of S and S − 1.]
b) How many bitwise AND operations are needed to find the number of 1 bits in a string S using the algorithm in pan (a)?
ANSWER:
Solution:
Step 1
In this problem we need to show that the given algorithm counts the number of 1’s in a bit string. We also need to find the number of AND operations are computed to find the number of 1 bits in a bit string using the given algorithm.