<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
 lang="en" dir="ltr">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>
    Bitset Constructors    [C++ Reference]
  </title>

  <meta name="generator" content="DokuWiki Release 2009-12-25c &quot;Lemming&quot;" />
<meta name="robots" content="index,follow" />
<meta name="date" content="2009-10-10T06:41:45-0700" />
<meta name="keywords" content="stl,bitset,bitset_constructors" />
<link rel="search" type="application/opensearchdescription+xml" href="/wiki/lib/exe/opensearch.php" title="C++ Reference" />
<link rel="start" href="/wiki/" />
<link rel="contents" href="/wiki/stl/bitset/bitset_constructors?do=index" title="Index" />
<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="/wiki/feed.php" />
<link rel="alternate" type="application/rss+xml" title="Current Namespace" href="/wiki/feed.php?mode=list&amp;ns=stl:bitset" />
<link rel="edit" title="Edit this page" href="/wiki/stl/bitset/bitset_constructors?do=edit" />
<link rel="alternate" type="text/html" title="Plain HTML" href="/wiki/_export/xhtml/stl/bitset/bitset_constructors" />
<link rel="alternate" type="text/plain" title="Wiki Markup" href="/wiki/_export/raw/stl/bitset/bitset_constructors" />
<link rel="canonical" href="http://www.cppreference.com/wiki/stl/bitset/bitset_constructors" />
<link rel="stylesheet" media="all" type="text/css" href="/wiki/lib/exe/css.php?s=all&amp;t=custom1&amp;tseed=1272971091" />
<link rel="stylesheet" media="screen" type="text/css" href="/wiki/lib/exe/css.php?t=custom1&amp;tseed=1272971091" />
<link rel="stylesheet" media="print" type="text/css" href="/wiki/lib/exe/css.php?s=print&amp;t=custom1&amp;tseed=1272971091" />
<script type="text/javascript" charset="utf-8" ><!--//--><![CDATA[//><!--
var NS='stl:bitset';var JSINFO = {"id":"stl:bitset:bitset_constructors","namespace":"stl:bitset"};
//--><!]]></script>
<script type="text/javascript" charset="utf-8" src="/wiki/lib/exe/js.php?tseed=1272971091" ></script>

  <link rel="shortcut icon" href="/wiki/lib/tpl/custom1/images/favicon.png" />

  </head>

<body>
<div class="dokuwiki">
  
  <div class="stylehead">

    <div class="header">
      <div class="pagename">
        [[<a href="../../stl/bitset/bitset.html"  title="Backlinks">Bitset Constructors</a>]]
      </div>
      <div class="logo">
        <a href="http://www.cppreference.com"  name="dokuwiki__top" id="dokuwiki__top" accesskey="h" title="[ALT+H]">C++ Reference</a>      </div>

      <div class="clearer"></div>
    </div>

    
    
        <div class="breadcrumbs">
      <span class="bchead">You are here: </span><a href="../../start.html"  title="start">C++ Reference</a> &raquo; <a href="../../stl/start.html"  title="stl:start">C++ Standard Template Library</a> &raquo; <a href="../../stl/bitset/start.html"  title="stl:bitset:start">C++ Bitsets</a> &raquo; <a href="../../stl/bitset/bitset.html"  title="stl:bitset:bitset_constructors">Bitset Constructors</a>    </div>
    
  </div>

<div class="plugin_translation"><span>Translations of this page<sup><a href="../../localization.html" class="wikilink1" title="localization">?</a></sup>:</span> <ul>  <li><div class="li"><span class="curid"><a href="../../stl/bitset/bitset.html" class="wikilink1" title="stl:bitset:bitset_constructors">en</a></span></div></li>  <li><div class="li"><a href="../../br-pt/stl/bitset/bitset.html" class="wikilink2" title="br-pt:stl:bitset:bitset_constructors" rel="nofollow">br-pt</a></div></li>  <li><div class="li"><a href="../../cn/stl/bitset/bitset.html" class="wikilink2" title="cn:stl:bitset:bitset_constructors" rel="nofollow">cn</a></div></li>  <li><div class="li"><a href="../../cz/stl/bitset/bitset.html" class="wikilink2" title="cz:stl:bitset:bitset_constructors" rel="nofollow">cz</a></div></li>  <li><div class="li"><a href="../../de/stl/bitset/bitset.html" class="wikilink2" title="de:stl:bitset:bitset_constructors" rel="nofollow">de</a></div></li>  <li><div class="li"><a href="../../es/stl/bitset/bitset.html" class="wikilink2" title="es:stl:bitset:bitset_constructors" rel="nofollow">es</a></div></li>  <li><div class="li"><a href="../../fr/stl/bitset/bitset.html" class="wikilink2" title="fr:stl:bitset:bitset_constructors" rel="nofollow">fr</a></div></li>  <li><div class="li"><a href="../../it/stl/bitset/bitset.html" class="wikilink1" title="it:stl:bitset:bitset_constructors">it</a></div></li>  <li><div class="li"><a href="../../jp/stl/bitset/bitset.html" class="wikilink2" title="jp:stl:bitset:bitset_constructors" rel="nofollow">jp</a></div></li>  <li><div class="li"><a href="../../nl/stl/bitset/bitset.html" class="wikilink2" title="nl:stl:bitset:bitset_constructors" rel="nofollow">nl</a></div></li>  <li><div class="li"><a href="../../pl/stl/bitset/bitset.html" class="wikilink2" title="pl:stl:bitset:bitset_constructors" rel="nofollow">pl</a></div></li>  <li><div class="li"><a href="../../ro/stl/bitset/bitset.html" class="wikilink2" title="ro:stl:bitset:bitset_constructors" rel="nofollow">ro</a></div></li>  <li><div class="li"><a href="../../ru/stl/bitset/bitset.html" class="wikilink2" title="ru:stl:bitset:bitset_constructors" rel="nofollow">ru</a></div></li>  <li><div class="li"><a href="../../sk/stl/bitset/bitset.html" class="wikilink2" title="sk:stl:bitset:bitset_constructors" rel="nofollow">sk</a></div></li>  <li><div class="li"><a href="../../tr/stl/bitset/bitset.html" class="wikilink2" title="tr:stl:bitset:bitset_constructors" rel="nofollow">tr</a></div></li>  <li><div class="li"><a href="../../tw/stl/bitset/bitset.html" class="wikilink2" title="tw:stl:bitset:bitset_constructors" rel="nofollow">tw</a></div></li></ul></div>
  
  
  <div class="page">

    <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
    <!-- wikipage start -->
    


<h2><a name="bitset_constructors" id="bitset_constructors">Bitset Constructors</a></h2>
<div class="level2">

<p>
Syntax:
</p>
<pre class="c code c++" style="font-family:monospace;">    <span class="co2">#include &lt;bitset&gt;</span>
    bitset<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    bitset<span class="br0">&#40;</span> <span class="kw4">unsigned</span> <span class="kw4">long</span> val <span class="br0">&#41;</span><span class="sy0">;</span>
    explicit bitset<span class="br0">&#40;</span> <span class="kw4">const</span> string<span class="sy0">&amp;</span> str<span class="sy0">,</span> <span class="kw4">string</span><span class="sy0">::</span><span class="me2">size_type</span> pos <span class="sy0">=</span> 0<span class="sy0">,</span> <span class="kw4">string</span><span class="sy0">::</span><span class="me2">size_type</span> n <span class="sy0">=</span> <span class="kw4">string</span><span class="sy0">::</span><span class="me2">npos</span> <span class="br0">&#41;</span><span class="sy0">;</span></pre>
<p>
Bitsets can be constructed with default values, from the bits in an unsigned long, or from string.

</p>
<ul>
<li class="level1"><div class="li"> The default value of a bit is 0.</div>
</li>
<li class="level1"><div class="li"> When an unsigned long argument is supplied, each bit in the integer is used to initialize the corresponding bit in the bitset (if any).</div>
</li>
<li class="level1"><div class="li"> A string argument does the same, except that the character &#039;0&#039; gives the bitvalue 0, the character &#039;1&#039; gives the bitvalue 1, and other characters cause an invalid_argument exception to be thrown. Note that this constructor is explicit. So no implicit type conversion from const char* to string is provided.</div>
</li>
</ul>

<p>

When creating bitsets, the number given in the place of the template determines
how long the bitset is.
</p>

<p>
For example, the following code creates two bitsets and displays them:
</p>
<pre class="c code c++" style="font-family:monospace;"> <span class="co1">// create a bitset that is 8 bits long</span>
 bitset<span class="sy0">&lt;</span>8<span class="sy0">&gt;</span> bs<span class="sy0">;</span>
 <span class="co1">// display that bitset</span>
 <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> <span class="sy0">&lt;&lt;</span> bs <span class="sy0">&lt;&lt;</span> endl<span class="sy0">;</span>
&nbsp;
 <span class="co1">// create a bitset out of a number</span>
 bitset<span class="sy0">&lt;</span>8<span class="sy0">&gt;</span> bs2<span class="br0">&#40;</span> 131 <span class="br0">&#41;</span><span class="sy0">;</span>
 <span class="co1">// display that bitset, too</span>
 <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> <span class="sy0">&lt;&lt;</span> bs2 <span class="sy0">&lt;&lt;</span> endl<span class="sy0">;</span>
&nbsp;
 <span class="co1">// create a bitset out of a string</span>
 bitset<span class="sy0">&lt;</span>8<span class="sy0">&gt;</span> bs3<span class="br0">&#40;</span><span class="kw4">string</span><span class="br0">&#40;</span><span class="st0">&quot;10000000&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
 <span class="co1">// display the integer value of that bitset</span>
 <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> <span class="sy0">&lt;&lt;</span> bs3.<span class="me1">to_ulong</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">&lt;&lt;</span> endl<span class="sy0">;</span></pre>
<p>
When run, this code displays the following output:

</p>
<pre class="c code c++" style="font-family:monospace;"> 00000000
 <span class="nu0">10000011</span>
 <span class="nu0">128</span></pre>
</div>
<div class="secedit"><form class="button btn_secedit" method="post" action="/wiki/stl/bitset/bitset_constructors"><div class="no"><input type="hidden" name="do" value="edit" /><input type="hidden" name="lines" value="3-" /><input type="hidden" name="rev" value="1255182105" /><input type="submit" value="Edit" class="button" title="Bitset Constructors" /></div></form></div>
    <!-- wikipage stop -->
  </div>

  <div class="clearer">&nbsp;</div>

  
  <div class="stylefoot">

    <div class="meta">
      <div class="user">
              </div>
      <!--
      <div class="doc">
        stl/bitset/bitset_constructors.txt &middot; Last modified: 10/10/2009 06:41 by 124.255.102.233      </div>
      -->
    </div>

   
    <div class="bar" id="bar__bottom">
      <div class="bar-left" id="bar__bottomleft">
        <a href="../../stl/bitset/bitset.html"  class="action edit" accesskey="e" rel="nofollow">Edit this page</a> &#149;
        <a href="../../stl/bitset/bitset.html"  class="action revisions" accesskey="o" rel="nofollow">Old revisions</a>      </div>
      <div class="bar-right" id="bar__bottomright">
         &#149;
         &#149;
         &#149;
        <a href="../../stl/bitset/bitset.html"  class="action login" rel="nofollow">Login</a> &#149;
        <a href="../../stl/bitset/bitset.html"  class="action index" accesskey="x" rel="nofollow">Index</a> &#149;
        <a href="../../stl/bitset/bitset.html"  class="action recent" accesskey="r" rel="nofollow">Recent changes</a> &#149;
        <a  href="../../feed.php.html" title="Recent changes RSS feed">RSS</a> &#149;
        <a href='http://creativecommons.org/licenses/by/3.0/us/' title='Creative Commons license'>cc</a> &#149;
        <form action="/wiki/" accept-charset="utf-8" class="search" id="dw__search"><div class="no"><input type="hidden" name="do" value="search" /><input type="text" id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" /><input type="submit" value="Search" class="button" title="Search" /><div id="qsearch__out" class="ajax_qsearch JSpopup"></div></div></form>&nbsp;
      </div>
      <div class="clearer"></div>
    </div>

  </div>

</div>

<div class="no"><img src="/wiki/lib/exe/indexer.php?id=stl%3Abitset%3Abitset_constructors&amp;1273199055" width="1" height="1" alt=""  /></div>
</body>
</html>