<!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 "Lemming"" />
<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&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&t=custom1&tseed=1272971091" />
<link rel="stylesheet" media="screen" type="text/css" href="/wiki/lib/exe/css.php?t=custom1&tseed=1272971091" />
<link rel="stylesheet" media="print" type="text/css" href="/wiki/lib/exe/css.php?s=print&t=custom1&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="breadcrumbs">
<span class="bchead">You are here: </span><a href="../../start.html" title="start">C++ Reference</a> » <a href="../../stl/start.html" title="stl:start">C++ Standard Template Library</a> » <a href="../../stl/bitset/start.html" title="stl:bitset:start">C++ Bitsets</a> » <a href="../../stl/bitset/bitset.html" title="stl:bitset:bitset_constructors">Bitset Constructors</a> </div>
</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 <bitset></span>
bitset<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span>
bitset<span class="br0">(</span> <span class="kw4">unsigned</span> <span class="kw4">long</span> val <span class="br0">)</span><span class="sy0">;</span>
explicit bitset<span class="br0">(</span> <span class="kw4">const</span> string<span class="sy0">&</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">)</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 '0' gives the bitvalue 0, the character '1' 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"><</span>8<span class="sy0">></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"><<</span> bs <span class="sy0"><<</span> endl<span class="sy0">;</span>
<span class="co1">// create a bitset out of a number</span>
bitset<span class="sy0"><</span>8<span class="sy0">></span> bs2<span class="br0">(</span> 131 <span class="br0">)</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"><<</span> bs2 <span class="sy0"><<</span> endl<span class="sy0">;</span>
<span class="co1">// create a bitset out of a string</span>
bitset<span class="sy0"><</span>8<span class="sy0">></span> bs3<span class="br0">(</span><span class="kw4">string</span><span class="br0">(</span><span class="st0">"10000000"</span><span class="br0">)</span><span class="br0">)</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"><<</span> bs3.<span class="me1">to_ulong</span><span class="br0">(</span><span class="br0">)</span> <span class="sy0"><<</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>
<!-- wikipage stop -->
</div>
<div class="clearer"> </div>
<div class="stylefoot">
<div class="meta">
<div class="user">
</div>
<!--
<div class="doc">
stl/bitset/bitset_constructors.txt · Last modified: 10/10/2009 06:41 by 124.255.102.233 </div>
-->
</div>
</div></div></body>
</html>