Show that this algorithm determines the number of 1 bits in the bit string S:

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter4: Making Decisions
Section: Chapter Questions
Problem 9RQ
icon
Related questions
Question

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 bits to the
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 part (a)?

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Single source shortest path
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage