Discrete Mathematics and Its Applications, Seventh Edition

Published by McGraw-Hill Education
ISBN 10: 0073383090
ISBN 13: 978-0-07338-309-5

Chapter 3 - Section 3.1 - Algorithms - Exercises - Page 203: 25

Answer

procedure bitcount $($ string: a bit string consisting of $1$s and $0$s) count $:= 0$ Split string into a set of characters $c_1, c_2, c_3, ... c_n$ for $i:=1$ to $n$ $\space\space\space$if $c_i$ is equal to $1$ $\space\space\space$$\space\space\space$count $:=$ count + $1$ return count

Work Step by Step

This algorithm attempts to count the number of $1$s in a bit string. It does this by first splitting the string into a set of characters and then individually checking if each character is a $1$ or not. If it is, it increments $1$ to the counter. It then returns the counter, representing the number of $1$s encountered.
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.