You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<trclass="memdesc:a86c98dc299e4db28b73e08309d977e62"><tdclass="mdescLeft"> </td><tdclass="mdescRight">The main function implements set bit count. <br/></td></tr>
<trclass="memdesc:aea9e56e21fc34d417d702319303a285a"><tdclass="mdescLeft"> </td><tdclass="mdescRight">The main function implements set bit count. <br/></td></tr>
<dlclass="section return"><dt>Returns</dt><dd>total number of set-bits in the binary representation of number <spanclass="tt">n</span></dd></dl>
180
180
181
181
<pclass="definition">Definition at line <aclass="el" href="../../da/db8/count__of__set__bits_8cpp_source.html#l00038">38</a> of file <aclass="el" href="../../da/db8/count__of__set__bits_8cpp_source.html">count_of_set_bits.cpp</a>.</p>
182
-
<divclass="fragment"><divclass="line"><spanclass="lineno"> 39</span> { <spanclass="comment">// int64_t is preferred over int so that</span></div>
182
+
<divclass="fragment"><divclass="line"><spanclass="lineno"> 39</span>{ <spanclass="comment">// uint64_t is preferred over int so that</span></div>
183
183
<divclass="line"><spanclass="lineno"> 40</span><spanclass="comment">// no Overflow can be there.</span></div>
<divclass="line"><spanclass="lineno"> 42</span><spanclass="keywordtype">int</span> count = 0; <spanclass="comment">// "count" variable is used to count number of set-bits('1')</span></div>
186
-
<divclass="line"><spanclass="lineno"> 43</span><spanclass="comment">// in binary representation of number 'n'</span></div>
<divclass="line"><spanclass="lineno"> 49</span><spanclass="comment">// Why this algorithm is better than the standard one?</span></div>
193
-
<divclass="line"><spanclass="lineno"> 50</span><spanclass="comment">// Because this algorithm runs the same number of times as the number of</span></div>
194
-
<divclass="line"><spanclass="lineno"> 51</span><spanclass="comment">// set-bits in it. Means if my number is having "3" set bits, then this</span></div>
195
-
<divclass="line"><spanclass="lineno"> 52</span><spanclass="comment">// while loop will run only "3" times!!</span></div>
<divclass="line"><spanclass="lineno"> 41</span><spanclass="comment">//It's preferred over int64_t because it Guarantees that inputs are always non-negative, </span></div>
185
+
<divclass="line"><spanclass="lineno"> 42</span><spanclass="comment">//which matches the algorithmic problem statement.</span></div>
186
+
<divclass="line"><spanclass="lineno"> 43</span><spanclass="comment">//set bit counting is conceptually defined only for non-negative numbers.</span></div>
187
+
<divclass="line"><spanclass="lineno"> 44</span><spanclass="comment">//Provides a type Safety: Using an unsigned type helps prevent accidental negative values,</span></div>
<divclass="line"><spanclass="lineno"> 46</span> std::uint64_t count = 0; <spanclass="comment">// "count" variable is used to count number of set-bits('1')</span></div>
190
+
<divclass="line"><spanclass="lineno"> 47</span><spanclass="comment">// in binary representation of number 'n'</span></div>
191
+
<divclass="line"><spanclass="lineno"> 48</span><spanclass="comment">//Count is uint64_t because it Prevents theoretical overflow if someone passes very large integers.</span></div>
192
+
<divclass="line"><spanclass="lineno"> 49</span><spanclass="comment">// Behavior stays the same for all normal inputs.</span></div>
193
+
<divclass="line"><spanclass="lineno"> 50</span><spanclass="comment">// Safer for edge cases.</span></div>
<divclass="line"><spanclass="lineno"> 57</span><spanclass="comment">// Why this algorithm is better than the standard one?</span></div>
201
+
<divclass="line"><spanclass="lineno"> 58</span><spanclass="comment">// Because this algorithm runs the same number of times as the number of</span></div>
202
+
<divclass="line"><spanclass="lineno"> 59</span><spanclass="comment">// set-bits in it. Means if my number is having "3" set bits, then this</span></div>
203
+
<divclass="line"><spanclass="lineno"> 60</span><spanclass="comment">// while loop will run only "3" times!!</span></div>
<dlclass="section return"><dt>Returns</dt><dd>0 on exit </dd></dl>
217
225
218
-
<pclass="definition">Definition at line <aclass="el" href="../../da/db8/count__of__set__bits_8cpp_source.html#l00080">80</a> of file <aclass="el" href="../../da/db8/count__of__set__bits_8cpp_source.html">count_of_set_bits.cpp</a>.</p>
<pclass="definition">Definition at line <aclass="el" href="../../da/db8/count__of__set__bits_8cpp_source.html#l00088">88</a> of file <aclass="el" href="../../da/db8/count__of__set__bits_8cpp_source.html">count_of_set_bits.cpp</a>.</p>
<pclass="definition">Definition at line <aclass="el" href="../../da/db8/count__of__set__bits_8cpp_source.html#l00057">57</a> of file <aclass="el" href="../../da/db8/count__of__set__bits_8cpp_source.html">count_of_set_bits.cpp</a>.</p>
<divclass="ttc" id="acount__of__set__bits_8cpp_html_a86c98dc299e4db28b73e08309d977e62"><divclass="ttname"><ahref="#a86c98dc299e4db28b73e08309d977e62">bit_manipulation::count_of_set_bits::countSetBits</a></div><divclass="ttdeci">std::uint64_t countSetBits(std ::int64_t n)</div><divclass="ttdoc">The main function implements set bit count.</div><divclass="ttdef"><b>Definition</b><ahref="../../da/db8/count__of__set__bits_8cpp_source.html#l00038">count_of_set_bits.cpp:38</a></div></div>
258
+
<pclass="definition">Definition at line <aclass="el" href="../../da/db8/count__of__set__bits_8cpp_source.html#l00065">65</a> of file <aclass="el" href="../../da/db8/count__of__set__bits_8cpp_source.html">count_of_set_bits.cpp</a>.</p>
<divclass="ttc" id="acount__of__set__bits_8cpp_html_aea9e56e21fc34d417d702319303a285a"><divclass="ttname"><ahref="#aea9e56e21fc34d417d702319303a285a">bit_manipulation::count_of_set_bits::countSetBits</a></div><divclass="ttdeci">std::uint64_t countSetBits(std ::uint64_t n)</div><divclass="ttdoc">The main function implements set bit count.</div><divclass="ttdef"><b>Definition</b><ahref="../../da/db8/count__of__set__bits_8cpp_source.html#l00038">count_of_set_bits.cpp:38</a></div></div>
0 commit comments