<!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 Operators [C++ Reference]
</title>
<meta name="generator" content="DokuWiki Release 2009-12-25c "Lemming"" />
<meta name="robots" content="index,follow" />
<meta name="date" content="2010-04-08T14:19:55-0700" />
<meta name="keywords" content="stl,bitset,bitset_operators" />
<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_operators?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_operators?do=edit" />
<link rel="alternate" type="text/html" title="Plain HTML" href="/wiki/_export/xhtml/stl/bitset/bitset_operators" />
<link rel="alternate" type="text/plain" title="Wiki Markup" href="/wiki/_export/raw/stl/bitset/bitset_operators" />
<link rel="canonical" href="http://www.cppreference.com/wiki/stl/bitset/bitset_operators" />
<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_operators","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/operator.html" title="stl:bitset:bitset_operators">Bitset Operators</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_operators" id="bitset_operators">Bitset Operators</a></h2>
<div class="level2">
<p>
Syntax:
</p>
<pre class="c code c++" style="font-family:monospace;"> <span class="co2">#include <bitset></span>
<span class="sy0">!=,</span> <span class="sy0">==,</span> <span class="sy0">&=,</span> <span class="sy0">^=,</span> <span class="sy0">|=,</span> ~<span class="sy0">,</span> <span class="sy0"><<=,</span> <span class="sy0">>>=,</span> <span class="br0">[</span><span class="br0">]</span><span class="sy0">,</span> <span class="sy0">=</span></pre>
<p>
These operators all work with bitsets. They can be described as follows:
</p>
<table class="inline">
<tr class="row0">
<th class="col0"> Operator </th><th class="col1"> Description </th>
</tr>
<tr class="row1">
<td class="col0 centeralign"> <code>!=</code> </td><td class="col1">returns true if the two bitsets are not equal</td>
</tr>
<tr class="row2">
<td class="col0 centeralign"> <code>==</code> </td><td class="col1">returns true if the two bitsets are equal</td>
</tr>
<tr class="row3">
<td class="col0 centeralign"> <code>&=</code> </td><td class="col1">performs the AND operation on the two bitsets</td>
</tr>
<tr class="row4">
<td class="col0 centeralign"> <code>&</code> </td><td class="col1">performs the AND operation on the two bitsets</td>
</tr>
<tr class="row5">
<td class="col0 centeralign"> <code>^=</code> </td><td class="col1">performs the XOR operation on the two bitsets</td>
</tr>
<tr class="row6">
<td class="col0 centeralign"> <code>^</code> </td><td class="col1">performs the XOR operation on the two bitsets</td>
</tr>
<tr class="row7">
<td class="col0 centeralign"> <code>|=</code> </td><td class="col1">performs the OR operation on the two bitsets</td>
</tr>
<tr class="row8">
<td class="col0 centeralign"> <code>|</code> </td><td class="col1">performs the OR operation on the two bitsets</td>
</tr>
<tr class="row9">
<td class="col0 centeralign"> <code><<=</code> </td><td class="col1">shifts the bitset to the left</td>
</tr>
<tr class="row10">
<td class="col0 centeralign"> <code><<</code> </td><td class="col1">creates a copy of the bitset shifted to the left</td>
</tr>
<tr class="row11">
<td class="col0 centeralign"> <code>>>=</code> </td><td class="col1">shifts the bitset to the right</td>
</tr>
<tr class="row12">
<td class="col0 centeralign"> <code>>></code> </td><td class="col1">creates a copy of the bitset shifted to the right</td>
</tr>
<tr class="row13">
<td class="col0 centeralign"> <code>~</code> </td><td class="col1">creates a reversed copy of the bitset (same as calling flip() on a copy)</td>
</tr>
<tr class="row14">
<td class="col0 centeralign"> <code>b[x]</code> </td><td class="col1">returns a reference to the xth bit in the bitset</td>
</tr>
<tr class="row15">
<td class="col0 centeralign"> <code>=</code> </td><td class="col1">take an unsigned long and put the corresponding bits in the bitset</td>
</tr>
</table>
<p>
For example, the following code creates a bitset and shifts it to the left 4
places:
</p>
<pre class="c code c++" style="font-family:monospace;"> <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> 131ul <span class="br0">)</span><span class="sy0">;</span>
<span class="co1">// you can do bitset<8> bs2; bs2 = 131ul;</span>
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> <span class="sy0"><<</span> <span class="st0">"bs2 is "</span> <span class="sy0"><<</span> bs2 <span class="sy0"><<</span> endl<span class="sy0">;</span>
<span class="co1">// shift the bitset to the left by 4 digits</span>
bs2 <span class="sy0"><<=</span> <span class="nu0">4</span><span class="sy0">;</span>
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> <span class="sy0"><<</span> <span class="st0">"now bs2 is "</span> <span class="sy0"><<</span> bs2 <span class="sy0"><<</span> endl<span class="sy0">;</span></pre>
<p>
When the above code is run, it displays:
</p>
<pre class="code">
bs2 is 10000011
now bs2 is 00110000</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_operators.txt · Last modified: 04/08/2010 14:19 by caspin </div>
-->
</div>
</div></div></body>
</html>