a) Show that this algorithm determines the number of 1

Chapter 3, Problem 10E

(choose chapter or problem)

Get Unlimited 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)?

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.

Add to cart


Study Tools You Might Need

Not The Solution You Need? Search for Your Answer Here:

×

Login

Login or Sign up for access to all of our study tools and educational content!

Forgot password?
Register Now

×

Register

Sign up for access to all content on our site!

Or login if you already have an account

×

Reset password

If you have an active account we’ll send you an e-mail for password recovery

Or login if you have your password back