Documentation for 8541bb3674

This commit is contained in:
realstealthninja
2025-09-27 04:47:42 +00:00
parent 444bd9e6be
commit 724be2dabd
6 changed files with 108 additions and 92 deletions

View File

@@ -139,8 +139,8 @@ Namespaces</h2></td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 id="header-func-members" class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a86c98dc299e4db28b73e08309d977e62" id="r_a86c98dc299e4db28b73e08309d977e62"><td class="memItemLeft" align="right" valign="top">std::uint64_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a> (std ::int64_t n)</td></tr>
<tr class="memdesc:a86c98dc299e4db28b73e08309d977e62"><td class="mdescLeft">&#160;</td><td class="mdescRight">The main function implements set bit count. <br /></td></tr>
<tr class="memitem:aea9e56e21fc34d417d702319303a285a" id="r_aea9e56e21fc34d417d702319303a285a"><td class="memItemLeft" align="right" valign="top">std::uint64_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a> (std ::uint64_t n)</td></tr>
<tr class="memdesc:aea9e56e21fc34d417d702319303a285a"><td class="mdescLeft">&#160;</td><td class="mdescRight">The main function implements set bit count. <br /></td></tr>
<tr class="memitem:aa8dca7b867074164d5f45b0f3851269d" id="r_aa8dca7b867074164d5f45b0f3851269d"><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#aa8dca7b867074164d5f45b0f3851269d">test</a> ()</td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4" id="r_ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function. <br /></td></tr>
@@ -154,8 +154,8 @@ Functions</h2></td></tr>
<p class="definition">Definition in file <a class="el" href="../../da/db8/count__of__set__bits_8cpp_source.html">count_of_set_bits.cpp</a>.</p>
</div><a name="doc-func-members" id="doc-func-members"></a><h2 id="header-doc-func-members" class="groupheader">Function Documentation</h2>
<a id="a86c98dc299e4db28b73e08309d977e62" name="a86c98dc299e4db28b73e08309d977e62"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a86c98dc299e4db28b73e08309d977e62">&#9670;&#160;</a></span>countSetBits()</h2>
<a id="aea9e56e21fc34d417d702319303a285a" name="aea9e56e21fc34d417d702319303a285a"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aea9e56e21fc34d417d702319303a285a">&#9670;&#160;</a></span>countSetBits()</h2>
<div class="memitem">
<div class="memproto">
@@ -163,7 +163,7 @@ Functions</h2></td></tr>
<tr>
<td class="memname">std::uint64_t bit_manipulation::count_of_set_bits::countSetBits </td>
<td>(</td>
<td class="paramtype">std ::int64_t</td> <td class="paramname"><span class="paramname"><em>n</em></span></td><td>)</td>
<td class="paramtype">std ::uint64_t</td> <td class="paramname"><span class="paramname"><em>n</em></span></td><td>)</td>
<td></td>
</tr>
</table>
@@ -179,21 +179,29 @@ Functions</h2></td></tr>
<dl class="section return"><dt>Returns</dt><dd>total number of set-bits in the binary representation of number <span class="tt">n</span> </dd></dl>
<p class="definition">Definition at line <a class="el" href="../../da/db8/count__of__set__bits_8cpp_source.html#l00038">38</a> of file <a class="el" href="../../da/db8/count__of__set__bits_8cpp_source.html">count_of_set_bits.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 39</span> { <span class="comment">// int64_t is preferred over int so that</span></div>
<div class="fragment"><div class="line"><span class="lineno"> 39</span> { <span class="comment">// uint64_t is preferred over int so that</span></div>
<div class="line"><span class="lineno"> 40</span> <span class="comment">// no Overflow can be there.</span></div>
<div class="line"><span class="lineno"> 41</span> </div>
<div class="line"><span class="lineno"> 42</span> <span class="keywordtype">int</span> count = 0; <span class="comment">// &quot;count&quot; variable is used to count number of set-bits(&#39;1&#39;)</span></div>
<div class="line"><span class="lineno"> 43</span> <span class="comment">// in binary representation of number &#39;n&#39;</span></div>
<div class="line"><span class="lineno"> 44</span> <span class="keywordflow">while</span> (n != 0) {</div>
<div class="line"><span class="lineno"> 45</span> ++count;</div>
<div class="line"><span class="lineno"> 46</span> n = (n &amp; (n - 1));</div>
<div class="line"><span class="lineno"> 47</span> }</div>
<div class="line"><span class="lineno"> 48</span> <span class="keywordflow">return</span> count;</div>
<div class="line"><span class="lineno"> 49</span> <span class="comment">// Why this algorithm is better than the standard one?</span></div>
<div class="line"><span class="lineno"> 50</span> <span class="comment">// Because this algorithm runs the same number of times as the number of</span></div>
<div class="line"><span class="lineno"> 51</span> <span class="comment">// set-bits in it. Means if my number is having &quot;3&quot; set bits, then this</span></div>
<div class="line"><span class="lineno"> 52</span> <span class="comment">// while loop will run only &quot;3&quot; times!!</span></div>
<div class="line"><span class="lineno"> 53</span>}</div>
<div class="line"><span class="lineno"> 41</span> <span class="comment">//It&#39;s preferred over int64_t because it Guarantees that inputs are always non-negative, </span></div>
<div class="line"><span class="lineno"> 42</span> <span class="comment">//which matches the algorithmic problem statement.</span></div>
<div class="line"><span class="lineno"> 43</span> <span class="comment">//set bit counting is conceptually defined only for non-negative numbers.</span></div>
<div class="line"><span class="lineno"> 44</span> <span class="comment">//Provides a type Safety: Using an unsigned type helps prevent accidental negative values,</span></div>
<div class="line"><span class="lineno"> 45</span> </div>
<div class="line"><span class="lineno"> 46</span> std::uint64_t count = 0; <span class="comment">// &quot;count&quot; variable is used to count number of set-bits(&#39;1&#39;)</span></div>
<div class="line"><span class="lineno"> 47</span> <span class="comment">// in binary representation of number &#39;n&#39;</span></div>
<div class="line"><span class="lineno"> 48</span> <span class="comment">//Count is uint64_t because it Prevents theoretical overflow if someone passes very large integers.</span></div>
<div class="line"><span class="lineno"> 49</span> <span class="comment">// Behavior stays the same for all normal inputs.</span></div>
<div class="line"><span class="lineno"> 50</span> <span class="comment">// Safer for edge cases.</span></div>
<div class="line"><span class="lineno"> 51</span> </div>
<div class="line"><span class="lineno"> 52</span> <span class="keywordflow">while</span> (n != 0) {</div>
<div class="line"><span class="lineno"> 53</span> ++count;</div>
<div class="line"><span class="lineno"> 54</span> n = (n &amp; (n - 1));</div>
<div class="line"><span class="lineno"> 55</span> }</div>
<div class="line"><span class="lineno"> 56</span> <span class="keywordflow">return</span> count;</div>
<div class="line"><span class="lineno"> 57</span> <span class="comment">// Why this algorithm is better than the standard one?</span></div>
<div class="line"><span class="lineno"> 58</span> <span class="comment">// Because this algorithm runs the same number of times as the number of</span></div>
<div class="line"><span class="lineno"> 59</span> <span class="comment">// set-bits in it. Means if my number is having &quot;3&quot; set bits, then this</span></div>
<div class="line"><span class="lineno"> 60</span> <span class="comment">// while loop will run only &quot;3&quot; times!!</span></div>
<div class="line"><span class="lineno"> 61</span>}</div>
</div><!-- fragment -->
</div>
</div>
@@ -215,11 +223,11 @@ Functions</h2></td></tr>
<p>Main function. </p>
<dl class="section return"><dt>Returns</dt><dd>0 on exit </dd></dl>
<p class="definition">Definition at line <a class="el" href="../../da/db8/count__of__set__bits_8cpp_source.html#l00080">80</a> of file <a class="el" href="../../da/db8/count__of__set__bits_8cpp_source.html">count_of_set_bits.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 80</span> {</div>
<div class="line"><span class="lineno"> 81</span> <a class="code hl_function" href="../../dd/d1e/generate__parentheses_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a>(); <span class="comment">// run self-test implementations</span></div>
<div class="line"><span class="lineno"> 82</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 83</span>}</div>
<p class="definition">Definition at line <a class="el" href="../../da/db8/count__of__set__bits_8cpp_source.html#l00088">88</a> of file <a class="el" href="../../da/db8/count__of__set__bits_8cpp_source.html">count_of_set_bits.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 88</span> {</div>
<div class="line"><span class="lineno"> 89</span> <a class="code hl_function" href="../../dd/d1e/generate__parentheses_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a>(); <span class="comment">// run self-test implementations</span></div>
<div class="line"><span class="lineno"> 90</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 91</span>}</div>
<div class="ttc" id="agenerate__parentheses_8cpp_html_aa8dca7b867074164d5f45b0f3851269d"><div class="ttname"><a href="../../dd/d1e/generate__parentheses_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a></div><div class="ttdeci">static void test()</div><div class="ttdoc">Self-test implementations.</div><div class="ttdef"><b>Definition</b> <a href="../../dd/d1e/generate__parentheses_8cpp_source.html#l00082">generate_parentheses.cpp:82</a></div></div>
</div><!-- fragment -->
</div>
@@ -247,27 +255,27 @@ Functions</h2></td></tr>
</table>
</div><div class="memdoc">
<p class="definition">Definition at line <a class="el" href="../../da/db8/count__of__set__bits_8cpp_source.html#l00057">57</a> of file <a class="el" href="../../da/db8/count__of__set__bits_8cpp_source.html">count_of_set_bits.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 57</span> {</div>
<div class="line"><span class="lineno"> 58</span> <span class="comment">// n = 4 return 1</span></div>
<div class="line"><span class="lineno"> 59</span> assert(<a class="code hl_function" href="#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a>(4) == 1);</div>
<div class="line"><span class="lineno"> 60</span> <span class="comment">// n = 6 return 2</span></div>
<div class="line"><span class="lineno"> 61</span> assert(<a class="code hl_function" href="#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a>(6) == 2);</div>
<div class="line"><span class="lineno"> 62</span> <span class="comment">// n = 13 return 3</span></div>
<div class="line"><span class="lineno"> 63</span> assert(<a class="code hl_function" href="#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a>(13) == 3);</div>
<div class="line"><span class="lineno"> 64</span> <span class="comment">// n = 9 return 2</span></div>
<div class="line"><span class="lineno"> 65</span> assert(<a class="code hl_function" href="#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a>(9) == 2);</div>
<div class="line"><span class="lineno"> 66</span> <span class="comment">// n = 15 return 4</span></div>
<div class="line"><span class="lineno"> 67</span> assert(<a class="code hl_function" href="#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a>(15) == 4);</div>
<div class="line"><span class="lineno"> 68</span> <span class="comment">// n = 25 return 3</span></div>
<div class="line"><span class="lineno"> 69</span> assert(<a class="code hl_function" href="#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a>(25) == 3);</div>
<div class="line"><span class="lineno"> 70</span> <span class="comment">// n = 97 return 3</span></div>
<div class="line"><span class="lineno"> 71</span> assert(<a class="code hl_function" href="#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a>(97) == 3);</div>
<div class="line"><span class="lineno"> 72</span> <span class="comment">// n = 31 return 5</span></div>
<div class="line"><span class="lineno"> 73</span> assert(<a class="code hl_function" href="#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a>(31) == 5);</div>
<div class="line"><span class="lineno"> 74</span> std::cout &lt;&lt; <span class="stringliteral">&quot;All test cases successfully passed!&quot;</span> &lt;&lt; std::endl;</div>
<div class="line"><span class="lineno"> 75</span>}</div>
<div class="ttc" id="acount__of__set__bits_8cpp_html_a86c98dc299e4db28b73e08309d977e62"><div class="ttname"><a href="#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a></div><div class="ttdeci">std::uint64_t countSetBits(std ::int64_t n)</div><div class="ttdoc">The main function implements set bit count.</div><div class="ttdef"><b>Definition</b> <a href="../../da/db8/count__of__set__bits_8cpp_source.html#l00038">count_of_set_bits.cpp:38</a></div></div>
<p class="definition">Definition at line <a class="el" href="../../da/db8/count__of__set__bits_8cpp_source.html#l00065">65</a> of file <a class="el" href="../../da/db8/count__of__set__bits_8cpp_source.html">count_of_set_bits.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 65</span> {</div>
<div class="line"><span class="lineno"> 66</span> <span class="comment">// n = 4 return 1</span></div>
<div class="line"><span class="lineno"> 67</span> assert(<a class="code hl_function" href="#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a>(4) == 1);</div>
<div class="line"><span class="lineno"> 68</span> <span class="comment">// n = 6 return 2</span></div>
<div class="line"><span class="lineno"> 69</span> assert(<a class="code hl_function" href="#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a>(6) == 2);</div>
<div class="line"><span class="lineno"> 70</span> <span class="comment">// n = 13 return 3</span></div>
<div class="line"><span class="lineno"> 71</span> assert(<a class="code hl_function" href="#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a>(13) == 3);</div>
<div class="line"><span class="lineno"> 72</span> <span class="comment">// n = 9 return 2</span></div>
<div class="line"><span class="lineno"> 73</span> assert(<a class="code hl_function" href="#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a>(9) == 2);</div>
<div class="line"><span class="lineno"> 74</span> <span class="comment">// n = 15 return 4</span></div>
<div class="line"><span class="lineno"> 75</span> assert(<a class="code hl_function" href="#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a>(15) == 4);</div>
<div class="line"><span class="lineno"> 76</span> <span class="comment">// n = 25 return 3</span></div>
<div class="line"><span class="lineno"> 77</span> assert(<a class="code hl_function" href="#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a>(25) == 3);</div>
<div class="line"><span class="lineno"> 78</span> <span class="comment">// n = 97 return 3</span></div>
<div class="line"><span class="lineno"> 79</span> assert(<a class="code hl_function" href="#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a>(97) == 3);</div>
<div class="line"><span class="lineno"> 80</span> <span class="comment">// n = 31 return 5</span></div>
<div class="line"><span class="lineno"> 81</span> assert(<a class="code hl_function" href="#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a>(31) == 5);</div>
<div class="line"><span class="lineno"> 82</span> std::cout &lt;&lt; <span class="stringliteral">&quot;All test cases successfully passed!&quot;</span> &lt;&lt; std::endl;</div>
<div class="line"><span class="lineno"> 83</span>}</div>
<div class="ttc" id="acount__of__set__bits_8cpp_html_aea9e56e21fc34d417d702319303a285a"><div class="ttname"><a href="#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a></div><div class="ttdeci">std::uint64_t countSetBits(std ::uint64_t n)</div><div class="ttdoc">The main function implements set bit count.</div><div class="ttdef"><b>Definition</b> <a href="../../da/db8/count__of__set__bits_8cpp_source.html#l00038">count_of_set_bits.cpp:38</a></div></div>
</div><!-- fragment -->
</div>
</div>

View File

@@ -1,5 +1,5 @@
var count__of__set__bits_8cpp =
[
[ "bit_manipulation::count_of_set_bits::countSetBits", "da/db8/count__of__set__bits_8cpp.html#a86c98dc299e4db28b73e08309d977e62", null ],
[ "bit_manipulation::count_of_set_bits::countSetBits", "da/db8/count__of__set__bits_8cpp.html#aea9e56e21fc34d417d702319303a285a", null ],
[ "main", "da/db8/count__of__set__bits_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4", null ]
];

View File

@@ -123,53 +123,61 @@ $(function(){initNavTree('da/db8/count__of__set__bits_8cpp_source.html','../../'
<div class="line"><a id="l00025" name="l00025"></a><span class="lineno"> 25</span><span class="keyword">namespace </span><a class="code hl_namespace" href="../../d7/d81/namespacebit__manipulation.html">bit_manipulation</a> {</div>
<div class="line"><a id="l00032" name="l00032"></a><span class="lineno"> 32</span><span class="keyword">namespace </span><a class="code hl_namespace" href="../../dd/dae/namespacecount__of__set__bits.html">count_of_set_bits</a> {</div>
<div class="foldopen" id="foldopen00038" data-start="{" data-end="}">
<div class="line"><a id="l00038" name="l00038"></a><span class="lineno"><a class="line" href="../../da/db8/count__of__set__bits_8cpp.html#a86c98dc299e4db28b73e08309d977e62"> 38</a></span>std::uint64_t <a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#a86c98dc299e4db28b73e08309d977e62">countSetBits</a>(</div>
<div class="line"><a id="l00039" name="l00039"></a><span class="lineno"> 39</span> std ::int64_t n) { <span class="comment">// int64_t is preferred over int so that</span></div>
<div class="line"><a id="l00038" name="l00038"></a><span class="lineno"><a class="line" href="../../da/db8/count__of__set__bits_8cpp.html#aea9e56e21fc34d417d702319303a285a"> 38</a></span>std::uint64_t <a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#aea9e56e21fc34d417d702319303a285a">countSetBits</a>(</div>
<div class="line"><a id="l00039" name="l00039"></a><span class="lineno"> 39</span> std ::uint64_t n) { <span class="comment">// uint64_t is preferred over int so that</span></div>
<div class="line"><a id="l00040" name="l00040"></a><span class="lineno"> 40</span> <span class="comment">// no Overflow can be there.</span></div>
<div class="line"><a id="l00041" name="l00041"></a><span class="lineno"> 41</span> </div>
<div class="line"><a id="l00042" name="l00042"></a><span class="lineno"> 42</span> <span class="keywordtype">int</span> count = 0; <span class="comment">// &quot;count&quot; variable is used to count number of set-bits(&#39;1&#39;)</span></div>
<div class="line"><a id="l00043" name="l00043"></a><span class="lineno"> 43</span> <span class="comment">// in binary representation of number &#39;n&#39;</span></div>
<div class="line"><a id="l00044" name="l00044"></a><span class="lineno"> 44</span> <span class="keywordflow">while</span> (n != 0) {</div>
<div class="line"><a id="l00045" name="l00045"></a><span class="lineno"> 45</span> ++count;</div>
<div class="line"><a id="l00046" name="l00046"></a><span class="lineno"> 46</span> n = (n &amp; (n - 1));</div>
<div class="line"><a id="l00047" name="l00047"></a><span class="lineno"> 47</span> }</div>
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno"> 48</span> <span class="keywordflow">return</span> count;</div>
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno"> 49</span> <span class="comment">// Why this algorithm is better than the standard one?</span></div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno"> 50</span> <span class="comment">// Because this algorithm runs the same number of times as the number of</span></div>
<div class="line"><a id="l00051" name="l00051"></a><span class="lineno"> 51</span> <span class="comment">// set-bits in it. Means if my number is having &quot;3&quot; set bits, then this</span></div>
<div class="line"><a id="l00052" name="l00052"></a><span class="lineno"> 52</span> <span class="comment">// while loop will run only &quot;3&quot; times!!</span></div>
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno"> 53</span>}</div>
<div class="line"><a id="l00041" name="l00041"></a><span class="lineno"> 41</span> <span class="comment">//It&#39;s preferred over int64_t because it Guarantees that inputs are always non-negative, </span></div>
<div class="line"><a id="l00042" name="l00042"></a><span class="lineno"> 42</span> <span class="comment">//which matches the algorithmic problem statement.</span></div>
<div class="line"><a id="l00043" name="l00043"></a><span class="lineno"> 43</span> <span class="comment">//set bit counting is conceptually defined only for non-negative numbers.</span></div>
<div class="line"><a id="l00044" name="l00044"></a><span class="lineno"> 44</span> <span class="comment">//Provides a type Safety: Using an unsigned type helps prevent accidental negative values,</span></div>
<div class="line"><a id="l00045" name="l00045"></a><span class="lineno"> 45</span> </div>
<div class="line"><a id="l00046" name="l00046"></a><span class="lineno"> 46</span> std::uint64_t count = 0; <span class="comment">// &quot;count&quot; variable is used to count number of set-bits(&#39;1&#39;)</span></div>
<div class="line"><a id="l00047" name="l00047"></a><span class="lineno"> 47</span> <span class="comment">// in binary representation of number &#39;n&#39;</span></div>
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno"> 48</span> <span class="comment">//Count is uint64_t because it Prevents theoretical overflow if someone passes very large integers.</span></div>
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno"> 49</span> <span class="comment">// Behavior stays the same for all normal inputs.</span></div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno"> 50</span> <span class="comment">// Safer for edge cases.</span></div>
<div class="line"><a id="l00051" name="l00051"></a><span class="lineno"> 51</span> </div>
<div class="line"><a id="l00052" name="l00052"></a><span class="lineno"> 52</span> <span class="keywordflow">while</span> (n != 0) {</div>
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno"> 53</span> ++count;</div>
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno"> 54</span> n = (n &amp; (n - 1));</div>
<div class="line"><a id="l00055" name="l00055"></a><span class="lineno"> 55</span> }</div>
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno"> 56</span> <span class="keywordflow">return</span> count;</div>
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno"> 57</span> <span class="comment">// Why this algorithm is better than the standard one?</span></div>
<div class="line"><a id="l00058" name="l00058"></a><span class="lineno"> 58</span> <span class="comment">// Because this algorithm runs the same number of times as the number of</span></div>
<div class="line"><a id="l00059" name="l00059"></a><span class="lineno"> 59</span> <span class="comment">// set-bits in it. Means if my number is having &quot;3&quot; set bits, then this</span></div>
<div class="line"><a id="l00060" name="l00060"></a><span class="lineno"> 60</span> <span class="comment">// while loop will run only &quot;3&quot; times!!</span></div>
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno"> 61</span>}</div>
</div>
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno"> 54</span>} <span class="comment">// namespace count_of_set_bits</span></div>
<div class="line"><a id="l00055" name="l00055"></a><span class="lineno"> 55</span>} <span class="comment">// namespace bit_manipulation</span></div>
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno"> 56</span> </div>
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno"> 57</span><span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code hl_function" href="../../dd/d1e/generate__parentheses_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a>() {</div>
<div class="line"><a id="l00058" name="l00058"></a><span class="lineno"> 58</span> <span class="comment">// n = 4 return 1</span></div>
<div class="line"><a id="l00059" name="l00059"></a><span class="lineno"> 59</span> assert(<a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a>(4) == 1);</div>
<div class="line"><a id="l00060" name="l00060"></a><span class="lineno"> 60</span> <span class="comment">// n = 6 return 2</span></div>
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno"> 61</span> assert(<a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a>(6) == 2);</div>
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno"> 62</span> <span class="comment">// n = 13 return 3</span></div>
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno"> 63</span> assert(<a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a>(13) == 3);</div>
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno"> 64</span> <span class="comment">// n = 9 return 2</span></div>
<div class="line"><a id="l00065" name="l00065"></a><span class="lineno"> 65</span> assert(<a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a>(9) == 2);</div>
<div class="line"><a id="l00066" name="l00066"></a><span class="lineno"> 66</span> <span class="comment">// n = 15 return 4</span></div>
<div class="line"><a id="l00067" name="l00067"></a><span class="lineno"> 67</span> assert(<a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a>(15) == 4);</div>
<div class="line"><a id="l00068" name="l00068"></a><span class="lineno"> 68</span> <span class="comment">// n = 25 return 3</span></div>
<div class="line"><a id="l00069" name="l00069"></a><span class="lineno"> 69</span> assert(<a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a>(25) == 3);</div>
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno"> 70</span> <span class="comment">// n = 97 return 3</span></div>
<div class="line"><a id="l00071" name="l00071"></a><span class="lineno"> 71</span> assert(<a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a>(97) == 3);</div>
<div class="line"><a id="l00072" name="l00072"></a><span class="lineno"> 72</span> <span class="comment">// n = 31 return 5</span></div>
<div class="line"><a id="l00073" name="l00073"></a><span class="lineno"> 73</span> assert(<a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a>(31) == 5);</div>
<div class="line"><a id="l00074" name="l00074"></a><span class="lineno"> 74</span> std::cout &lt;&lt; <span class="stringliteral">&quot;All test cases successfully passed!&quot;</span> &lt;&lt; std::endl;</div>
<div class="line"><a id="l00075" name="l00075"></a><span class="lineno"> 75</span>}</div>
<div class="foldopen" id="foldopen00080" data-start="{" data-end="}">
<div class="line"><a id="l00080" name="l00080"></a><span class="lineno"><a class="line" href="../../da/db8/count__of__set__bits_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4"> 80</a></span><span class="keywordtype">int</span> <a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a>() {</div>
<div class="line"><a id="l00081" name="l00081"></a><span class="lineno"> 81</span> <a class="code hl_function" href="../../dd/d1e/generate__parentheses_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a>(); <span class="comment">// run self-test implementations</span></div>
<div class="line"><a id="l00082" name="l00082"></a><span class="lineno"> 82</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno"> 62</span>} <span class="comment">// namespace count_of_set_bits</span></div>
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno"> 63</span>} <span class="comment">// namespace bit_manipulation</span></div>
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno"> 64</span> </div>
<div class="line"><a id="l00065" name="l00065"></a><span class="lineno"> 65</span><span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code hl_function" href="../../dd/d1e/generate__parentheses_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a>() {</div>
<div class="line"><a id="l00066" name="l00066"></a><span class="lineno"> 66</span> <span class="comment">// n = 4 return 1</span></div>
<div class="line"><a id="l00067" name="l00067"></a><span class="lineno"> 67</span> assert(<a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a>(4) == 1);</div>
<div class="line"><a id="l00068" name="l00068"></a><span class="lineno"> 68</span> <span class="comment">// n = 6 return 2</span></div>
<div class="line"><a id="l00069" name="l00069"></a><span class="lineno"> 69</span> assert(<a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a>(6) == 2);</div>
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno"> 70</span> <span class="comment">// n = 13 return 3</span></div>
<div class="line"><a id="l00071" name="l00071"></a><span class="lineno"> 71</span> assert(<a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a>(13) == 3);</div>
<div class="line"><a id="l00072" name="l00072"></a><span class="lineno"> 72</span> <span class="comment">// n = 9 return 2</span></div>
<div class="line"><a id="l00073" name="l00073"></a><span class="lineno"> 73</span> assert(<a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a>(9) == 2);</div>
<div class="line"><a id="l00074" name="l00074"></a><span class="lineno"> 74</span> <span class="comment">// n = 15 return 4</span></div>
<div class="line"><a id="l00075" name="l00075"></a><span class="lineno"> 75</span> assert(<a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a>(15) == 4);</div>
<div class="line"><a id="l00076" name="l00076"></a><span class="lineno"> 76</span> <span class="comment">// n = 25 return 3</span></div>
<div class="line"><a id="l00077" name="l00077"></a><span class="lineno"> 77</span> assert(<a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a>(25) == 3);</div>
<div class="line"><a id="l00078" name="l00078"></a><span class="lineno"> 78</span> <span class="comment">// n = 97 return 3</span></div>
<div class="line"><a id="l00079" name="l00079"></a><span class="lineno"> 79</span> assert(<a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a>(97) == 3);</div>
<div class="line"><a id="l00080" name="l00080"></a><span class="lineno"> 80</span> <span class="comment">// n = 31 return 5</span></div>
<div class="line"><a id="l00081" name="l00081"></a><span class="lineno"> 81</span> assert(<a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a>(31) == 5);</div>
<div class="line"><a id="l00082" name="l00082"></a><span class="lineno"> 82</span> std::cout &lt;&lt; <span class="stringliteral">&quot;All test cases successfully passed!&quot;</span> &lt;&lt; std::endl;</div>
<div class="line"><a id="l00083" name="l00083"></a><span class="lineno"> 83</span>}</div>
<div class="foldopen" id="foldopen00088" data-start="{" data-end="}">
<div class="line"><a id="l00088" name="l00088"></a><span class="lineno"><a class="line" href="../../da/db8/count__of__set__bits_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4"> 88</a></span><span class="keywordtype">int</span> <a class="code hl_function" href="../../da/db8/count__of__set__bits_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a>() {</div>
<div class="line"><a id="l00089" name="l00089"></a><span class="lineno"> 89</span> <a class="code hl_function" href="../../dd/d1e/generate__parentheses_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a>(); <span class="comment">// run self-test implementations</span></div>
<div class="line"><a id="l00090" name="l00090"></a><span class="lineno"> 90</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><a id="l00091" name="l00091"></a><span class="lineno"> 91</span>}</div>
</div>
<div class="ttc" id="acount__of__set__bits_8cpp_html_a86c98dc299e4db28b73e08309d977e62"><div class="ttname"><a href="../../da/db8/count__of__set__bits_8cpp.html#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a></div><div class="ttdeci">std::uint64_t countSetBits(std ::int64_t n)</div><div class="ttdoc">The main function implements set bit count.</div><div class="ttdef"><b>Definition</b> <a href="#l00038">count_of_set_bits.cpp:38</a></div></div>
<div class="ttc" id="acount__of__set__bits_8cpp_html_ae66f6b31b5ad750f1fe042a706a4e3d4"><div class="ttname"><a href="../../da/db8/count__of__set__bits_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a></div><div class="ttdeci">int main()</div><div class="ttdoc">Main function.</div><div class="ttdef"><b>Definition</b> <a href="#l00080">count_of_set_bits.cpp:80</a></div></div>
<div class="ttc" id="acount__of__set__bits_8cpp_html_ae66f6b31b5ad750f1fe042a706a4e3d4"><div class="ttname"><a href="../../da/db8/count__of__set__bits_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a></div><div class="ttdeci">int main()</div><div class="ttdoc">Main function.</div><div class="ttdef"><b>Definition</b> <a href="#l00088">count_of_set_bits.cpp:88</a></div></div>
<div class="ttc" id="acount__of__set__bits_8cpp_html_aea9e56e21fc34d417d702319303a285a"><div class="ttname"><a href="../../da/db8/count__of__set__bits_8cpp.html#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a></div><div class="ttdeci">std::uint64_t countSetBits(std ::uint64_t n)</div><div class="ttdoc">The main function implements set bit count.</div><div class="ttdef"><b>Definition</b> <a href="#l00038">count_of_set_bits.cpp:38</a></div></div>
<div class="ttc" id="agenerate__parentheses_8cpp_html_aa8dca7b867074164d5f45b0f3851269d"><div class="ttname"><a href="../../dd/d1e/generate__parentheses_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a></div><div class="ttdeci">static void test()</div><div class="ttdoc">Self-test implementations.</div><div class="ttdef"><b>Definition</b> <a href="../../dd/d1e/generate__parentheses_8cpp_source.html#l00082">generate_parentheses.cpp:82</a></div></div>
<div class="ttc" id="anamespacebit__manipulation_html"><div class="ttname"><a href="../../d7/d81/namespacebit__manipulation.html">bit_manipulation</a></div><div class="ttdoc">for std::string</div></div>
<div class="ttc" id="anamespacecount__of__set__bits_html"><div class="ttname"><a href="../../dd/dae/namespacecount__of__set__bits.html">count_of_set_bits</a></div><div class="ttdoc">Functions for the count sets bits implementation.</div></div>

View File

@@ -2255,8 +2255,8 @@
<a href="da/dac/n__queens__all__solution__optimised_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4"/>
<a href="da/dac/n__queens__all__solution__optimised_8cpp_source.html"/>
<a href="da/db8/count__of__set__bits_8cpp.html"/>
<a href="da/db8/count__of__set__bits_8cpp.html#a86c98dc299e4db28b73e08309d977e62"/>
<a href="da/db8/count__of__set__bits_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4"/>
<a href="da/db8/count__of__set__bits_8cpp.html#aea9e56e21fc34d417d702319303a285a"/>
<a href="da/db8/count__of__set__bits_8cpp_source.html"/>
<a href="da/dc3/linked__list_8cpp.html"/>
<a href="da/dc3/linked__list_8cpp.html#ab1a372fe1e605bc0e0987dcdd7361180"/>

View File

@@ -108,7 +108,7 @@ var searchData=
['count_5fpaths_5fdfs_105',['count_paths_dfs',['../df/dce/namespacegraph.html#af99fccdd19e8f223e8749561589a762b',1,'graph']]],
['countbitsflip_106',['countBitsFlip',['../d7/d56/count__bits__flip_8cpp.html#a2548486b6c3b80101e768562e687ef7b',1,'bit_manipulation::count_bits_flip']]],
['countinversion_107',['countInversion',['../d2/d26/count__inversions_8cpp.html#a3332498eabf6579ef059c0d0e9f4ec80',1,'sorting::inversion']]],
['countsetbits_108',['countSetBits',['../da/db8/count__of__set__bits_8cpp.html#a86c98dc299e4db28b73e08309d977e62',1,'bit_manipulation::count_of_set_bits']]],
['countsetbits_108',['countSetBits',['../da/db8/count__of__set__bits_8cpp.html#aea9e56e21fc34d417d702319303a285a',1,'bit_manipulation::count_of_set_bits']]],
['covenant_20code_20of_20conduct_109',['Contributor Covenant Code of Conduct',['../d3/dd7/md__c_o_d_e___o_f___c_o_n_d_u_c_t.html',1,'']]],
['cpu_20scheduling_20algorithms_110',['Cpu Scheduling Algorithms',['../d5/d88/md__d_i_r_e_c_t_o_r_y.html#autotoc_md48',1,'']]],
['create_5fhash_111',['create_hash',['../d9/d03/namespacestring__search.html#a8fb0bc932ba8b582c9f4c71338d050f8',1,'string_search']]],

View File

@@ -40,7 +40,7 @@ var searchData=
['count_5fpaths_5fdfs_37',['count_paths_dfs',['../df/dce/namespacegraph.html#af99fccdd19e8f223e8749561589a762b',1,'graph']]],
['countbitsflip_38',['countBitsFlip',['../d7/d56/count__bits__flip_8cpp.html#a2548486b6c3b80101e768562e687ef7b',1,'bit_manipulation::count_bits_flip']]],
['countinversion_39',['countInversion',['../d2/d26/count__inversions_8cpp.html#a3332498eabf6579ef059c0d0e9f4ec80',1,'sorting::inversion']]],
['countsetbits_40',['countSetBits',['../da/db8/count__of__set__bits_8cpp.html#a86c98dc299e4db28b73e08309d977e62',1,'bit_manipulation::count_of_set_bits']]],
['countsetbits_40',['countSetBits',['../da/db8/count__of__set__bits_8cpp.html#aea9e56e21fc34d417d702319303a285a',1,'bit_manipulation::count_of_set_bits']]],
['create_5fhash_41',['create_hash',['../d9/d03/namespacestring__search.html#a8fb0bc932ba8b582c9f4c71338d050f8',1,'string_search']]],
['create_5flist_42',['create_list',['../d1/df3/hash__search_8cpp.html#ad0831425f1389166a9518f422d0c6ec5',1,'hash_search.cpp']]],
['create_5fmatrix_43',['create_matrix',['../de/d75/qr__eigen__values_8cpp.html#a9bbf469d5525a816b0d6ca812119093d',1,'qr_eigen_values.cpp']]],