Documentation for 8b1eab204b

This commit is contained in:
realstealthninja
2024-11-04 12:43:05 +00:00
parent 4fb6e622e9
commit a7bccf7d01
6732 changed files with 153919 additions and 365711 deletions

View File

@@ -1,3 +1,4 @@
<!-- HTML header for doxygen 1.12.0-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
@@ -5,10 +6,15 @@
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.12.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C++: bit_manipulation/count_of_set_bits.cpp File Reference</title>
<title>TheAlgorithms/C++: bit_manipulation/count_of_set_bits.cpp File Reference</title>
<link rel="icon" href="../../favicon.svg" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@xpack-3rd-party/doxygen-awesome-css@2.2.0-1/doxygen-awesome-darkmode-toggle.js"></script>
<script type="text/javascript">
DoxygenAwesomeDarkModeToggle.init()
</script>
<script type="text/javascript" src="../../clipboard.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../navtreedata.js"></script>
@@ -18,14 +24,24 @@
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
<script type="text/javascript">
window.MathJax = {
options: {
ignoreHtmlClass: 'tex2jax_ignore',
processHtmlClass: 'tex2jax_process'
},
loader: {
load: ['[tex]/ams']
},
tex: {
macros: {},
packages: ['base','configmacros','ams']
}
};
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<script type="text/javascript" id="MathJax-script" async="async" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../doxygen-awesome.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
@@ -33,10 +49,11 @@ MathJax.Hub.Config({
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectlogo"><img alt="Logo" src="../../project_logo.png"/></td>
<td id="projectalign">
<div id="projectname">Algorithms_in_C++<span id="projectnumber">&#160;1.0.0</span>
<div id="projectname">TheAlgorithms/C++<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C++.</div>
<div id="projectbrief">All the algorithms implemented in C++</div>
</td>
</tr>
</tbody>
@@ -121,7 +138,9 @@ Include dependency graph for count_of_set_bits.cpp:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../dd/d9f/count__of__set__bits_8cpp__incl.svg" width="255" height="126"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div><table class="memberdecls">
</div>
<p><a href="../../da/db8/count__of__set__bits_8cpp_source.html">Go to the source code of this file.</a></p>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="namespaces" name="namespaces"></a>
Namespaces</h2></td></tr>
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">namespace &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d7/d81/namespacebit__manipulation.html">bit_manipulation</a></td></tr>
@@ -133,7 +152,7 @@ Namespaces</h2></td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 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"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint64_t</a>&#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="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="separator:a86c98dc299e4db28b73e08309d977e62"><td class="memSeparator" colspan="2">&#160;</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>
@@ -148,6 +167,8 @@ Functions</h2></td></tr>
<p>A binary number consists of two digits. They are 0 &amp; 1. Digit 1 is known as set bit in computer terms. Worst Case Time Complexity: O(log n) Space complexity: O(1) </p><dl class="section author"><dt>Author</dt><dd><a href="https://github.com/Swastyy" target="_blank">Swastika Gupta</a> </dd>
<dd>
<a href="https://github.com/prashant-th18" target="_blank">Prashant Thakur</a> </dd></dl>
<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><h2 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>
@@ -156,7 +177,7 @@ Functions</h2></td></tr>
<div class="memproto">
<table class="memname">
<tr>
<td class="memname"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint64_t</a> bit_manipulation::count_of_set_bits::countSetBits </td>
<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></td>
@@ -172,6 +193,8 @@ Functions</h2></td></tr>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>total number of set-bits in the binary representation of number <code>n</code> </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="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>
@@ -187,12 +210,7 @@ Functions</h2></td></tr>
<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><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/db8/count__of__set__bits_8cpp_a86c98dc299e4db28b73e08309d977e62_cgraph.svg" width="187" height="76"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div><!-- fragment -->
</div>
</div>
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
@@ -212,6 +230,8 @@ Here is the call graph for this function:</div>
<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> test(); <span class="comment">// run self-test implementations</span></div>
<div class="line"><span class="lineno"> 82</span> <span class="keywordflow">return</span> 0;</div>
@@ -241,6 +261,8 @@ Here is the call graph for this function:</div>
</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(bit_manipulation::count_of_set_bits::countSetBits(4) == 1);</div>
@@ -258,10 +280,8 @@ Here is the call graph for this function:</div>
<div class="line"><span class="lineno"> 71</span> assert(bit_manipulation::count_of_set_bits::countSetBits(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(bit_manipulation::count_of_set_bits::countSetBits(31) == 5);</div>
<div class="line"><span class="lineno"> 74</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;All test cases successfully passed!&quot;</span> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</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="abasic_ostream_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a></div></div>
<div class="ttc" id="aendl_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a></div><div class="ttdeci">T endl(T... args)</div></div>
</div><!-- fragment -->
</div>
</div>

View File

@@ -1,4 +0,0 @@
<map id="bit_manipulation::count_of_set_bits::countSetBits" name="bit_manipulation::count_of_set_bits::countSetBits">
<area shape="rect" id="Node000001" title="The main function implements set bit count." alt="" coords="5,29,181,70"/>
<area shape="poly" id="edge1_Node000001_Node000001" title=" " alt="" coords="59,29,58,19,63,10,75,5,93,3,112,5,124,11,121,16,111,10,93,8,77,10,67,14,63,20,65,28"/>
</map>

View File

@@ -1 +0,0 @@
a371fd3fbac51fe7453dcf76db9829e2

View File

@@ -1,57 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 12.1.2 (20240928.0832)
-->
<!-- Title: bit_manipulation::count_of_set_bits::countSetBits Pages: 1 -->
<svg width="140pt" height="57pt"
viewBox="0.00 0.00 139.50 56.50" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<svg id="main" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve">
<style type="text/css"><![CDATA[
.node, .edge {opacity: 0.7;}
.node.selected, .edge.selected {opacity: 1;}
.edge:hover path { stroke: red; }
.edge:hover polygon { stroke: red; fill: red; }
]]></style>
<script type="application/ecmascript" xlink:href="../../svg.min.js"/>
<svg id="graph" class="graph">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 52.5)">
<title>bit_manipulation::count_of_set_bits::countSetBits</title>
<!-- Node1 -->
<g id="Node000001" class="node">
<title>Node1</title>
<g id="a_Node000001"><a xlink:title="The main function implements set bit count.">
<polygon fill="#999999" stroke="#666666" points="131.5,-30.5 0,-30.5 0,0 131.5,0 131.5,-30.5"/>
<text text-anchor="start" x="8" y="-17" font-family="Helvetica,sans-Serif" font-size="10.00">bit_manipulation::count</text>
<text text-anchor="middle" x="65.75" y="-5.75" font-family="Helvetica,sans-Serif" font-size="10.00">_of_set_bits::countSetBits</text>
</a>
</g>
</g>
<!-- Node1&#45;&gt;Node1 -->
<g id="edge1_Node000001_Node000001" class="edge">
<title>Node1&#45;&gt;Node1</title>
<g id="a_edge1_Node000001_Node000001"><a xlink:title=" ">
<path fill="none" stroke="#63b8ff" d="M42.45,-30.99C37.69,-40.06 45.46,-48.5 65.75,-48.5 76.85,-48.5 84.2,-45.97 87.8,-42.2"/>
<polygon fill="#63b8ff" stroke="#63b8ff" points="91.26,-42.82 88.88,-32.49 84.3,-42.04 91.26,-42.82"/>
</a>
</g>
</g>
</g>
</svg>
</svg>
<style type='text/css'>
<![CDATA[
[data-mouse-over-selected='false'] { opacity: 0.7; }
[data-mouse-over-selected='true'] { opacity: 1.0; }
]]>
</style>
<script type="application/ecmascript"><![CDATA[
document.addEventListener('DOMContentLoaded', (event) => {
highlightEdges();
highlightAdjacentNodes();
});
]]></script>
</svg>

Before

Width:  |  Height:  |  Size: 2.3 KiB

View File

@@ -1,31 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 12.1.2 (20240928.0832)
-->
<!-- Title: bit_manipulation::count_of_set_bits::countSetBits Pages: 1 -->
<svg width="140pt" height="57pt"
viewBox="0.00 0.00 139.50 56.50" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 52.5)">
<title>bit_manipulation::count_of_set_bits::countSetBits</title>
<!-- Node1 -->
<g id="Node000001" class="node">
<title>Node1</title>
<g id="a_Node000001"><a xlink:title="The main function implements set bit count.">
<polygon fill="#999999" stroke="#666666" points="131.5,-30.5 0,-30.5 0,0 131.5,0 131.5,-30.5"/>
<text text-anchor="start" x="8" y="-17" font-family="Helvetica,sans-Serif" font-size="10.00">bit_manipulation::count</text>
<text text-anchor="middle" x="65.75" y="-5.75" font-family="Helvetica,sans-Serif" font-size="10.00">_of_set_bits::countSetBits</text>
</a>
</g>
</g>
<!-- Node1&#45;&gt;Node1 -->
<g id="edge1_Node000001_Node000001" class="edge">
<title>Node1&#45;&gt;Node1</title>
<g id="a_edge1_Node000001_Node000001"><a xlink:title=" ">
<path fill="none" stroke="#63b8ff" d="M42.45,-30.99C37.69,-40.06 45.46,-48.5 65.75,-48.5 76.85,-48.5 84.2,-45.97 87.8,-42.2"/>
<polygon fill="#63b8ff" stroke="#63b8ff" points="91.26,-42.82 88.88,-32.49 84.3,-42.04 91.26,-42.82"/>
</a>
</g>
</g>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -0,0 +1,193 @@
<!-- HTML header for doxygen 1.12.0-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.12.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>TheAlgorithms/C++: bit_manipulation/count_of_set_bits.cpp Source File</title>
<link rel="icon" href="../../favicon.svg" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@xpack-3rd-party/doxygen-awesome-css@2.2.0-1/doxygen-awesome-darkmode-toggle.js"></script>
<script type="text/javascript">
DoxygenAwesomeDarkModeToggle.init()
</script>
<script type="text/javascript" src="../../clipboard.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../cookie.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/javascript">
window.MathJax = {
options: {
ignoreHtmlClass: 'tex2jax_ignore',
processHtmlClass: 'tex2jax_process'
},
loader: {
load: ['[tex]/ams']
},
tex: {
macros: {},
packages: ['base','configmacros','ams']
}
};
</script>
<script type="text/javascript" id="MathJax-script" async="async" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../doxygen-awesome.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectlogo"><img alt="Logo" src="../../project_logo.png"/></td>
<td id="projectalign">
<div id="projectname">TheAlgorithms/C++<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">All the algorithms implemented in C++</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.12.0 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
/* @license-end */
</script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() { codefold.init(1); });
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('../../',true,false,'search.php','Search',true);
$(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function(){initNavTree('da/db8/count__of__set__bits_8cpp_source.html','../../'); initResizable(true); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div class="header">
<div class="headertitle"><div class="title">count_of_set_bits.cpp</div></div>
</div><!--header-->
<div class="contents">
<a href="../../da/db8/count__of__set__bits_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a id="l00001" name="l00001"></a><span class="lineno"> 1</span> </div>
<div class="line"><a id="l00018" name="l00018"></a><span class="lineno"> 18</span><span class="preprocessor">#include &lt;cassert&gt;</span> </div>
<div class="line"><a id="l00019" name="l00019"></a><span class="lineno"> 19</span><span class="preprocessor">#include &lt;cstdint&gt;</span></div>
<div class="line"><a id="l00020" name="l00020"></a><span class="lineno"> 20</span><span class="preprocessor">#include &lt;iostream&gt;</span> </div>
<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="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>
<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> test() {</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(bit_manipulation::count_of_set_bits::countSetBits(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(bit_manipulation::count_of_set_bits::countSetBits(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(bit_manipulation::count_of_set_bits::countSetBits(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(bit_manipulation::count_of_set_bits::countSetBits(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(bit_manipulation::count_of_set_bits::countSetBits(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(bit_manipulation::count_of_set_bits::countSetBits(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(bit_manipulation::count_of_set_bits::countSetBits(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(bit_manipulation::count_of_set_bits::countSetBits(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> test(); <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="l00083" name="l00083"></a><span class="lineno"> 83</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="anamespacebit__manipulation_html"><div class="ttname"><a href="../../d7/d81/namespacebit__manipulation.html">bit_manipulation</a></div><div class="ttdoc">for assert</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>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_f3c4fbc4e901afa0a54d0623c5574aa7.html">bit_manipulation</a></li><li class="navelem"><a class="el" href="../../da/db8/count__of__set__bits_8cpp.html">count_of_set_bits.cpp</a></li>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.12.0 </li>
</ul>
</div>
</body>
</html>