This repository has been archived on 2019-12-23. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><title>Arduino - BitwiseCompound </title><linkrel='stylesheet'href='arduino.css'type='text/css'/><metaname="verify-v1"content="TtxFIEJAB6zdJ509wLxjnapQzKAMNm9u0Wj4ho6wxIY="/></head><body><divid="page"><!--PageHeaderFmt--><divid="pageheader"><divclass="title"><ahref="http://www.arduino.cc"/>Arduino</a></div><divclass="search"><!-- SiteSearch Google --><FORMmethod=GETaction="http://www.google.com/search"><inputtype=hiddenname=ievalue=UTF-8><inputtype=hiddenname=oevalue=UTF-8><INPUTTYPE=textname=qsize=25maxlength=255value=""><INPUTtype=submitname=btnGVALUE="search"><inputtype=hiddenname=domainsvalue="http://www.arduino.cc/"><inputtype=hiddenname=sitesearchvalue="http://www.arduino.cc/"></FORM><!-- SiteSearch Google --></div></div><!--/PageHeaderFmt--><!--PageLeftFmt--><divid="pagenav"style="text-align: right"><divstyle="float: left;"><p><aclass='wikilink'href='http://arduino.cc/en/Main/Buy'>Buy</a>
<pclass='vspace'></p><h2>compound bitwise AND (&=), compound bitwise OR (|=)</h2>
<p>The compound bitwise operators perform their calculations at the bit level of variables. They are often used to clear and set specific bits of a variable.
</p>
<pclass='vspace'></p><p>See the <aclass='wikilink'href='BitwiseAnd.html'>bitwise AND (&)</a> and <aclass='wikilink'href='BitwiseAnd.html'>bitwise OR (|)</a> operators for the details of their operation, and also the <aclass='urllink'href='http://www.arduino.cc/playground/Code/BitMath'rel='nofollow'>Bitmath Tutorial</a> for more information on bitwise operators.
</p>
<pclass='vspace'></p><h2>compound bitwise AND (&=)</h2>
<h4>Description </h4>
<p>The compound bitwise AND operator (&=) is often used with a variable and a constant to force particular bits in a variable to the LOW state (to 0). This is often referred to in programming guides as "clearing" or "resetting" bits.
</p>
<pclass='vspace'></p><h4>Syntax:</h4>
<pre>
x &= y; // equivalent to x = x & y;
</pre>
<pclass='vspace'></p><h4>Parameters</h4>
<p>x: a char, int or long variable<br/>y: an integer constant or char, int, or long
</p>
<pclass='vspace'></p><h4>Example:</h4>
<p>First, a review of the Bitwise AND (&) operator
</p><pre>
0 0 1 1 operand1
0 1 0 1 operand2
----------
0 0 0 1 (operand1 & operand2) - returned result
</pre>
<pclass='vspace'></p><p>Bits that are "bitwise <spanclass='wikiword'>ANDed</span>" with 0 are cleared to 0 so, if myByte is a byte variable,<br/>
<code>myByte & B00000000 = 0;</code>
</p>
<pclass='vspace'></p><p>Bits that are "bitwise <spanclass='wikiword'>ANDed</span>" with 1 are unchanged so, <br/>
<code>myByte & B11111111 = myByte;</code>
</p>
<pclass='vspace'></p><p>Note: because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with <aclass='wikilink'href='IntegerConstants.html'>constants.</a> The numbers are still the same value in other representations, they are just not as easy to understand. Also, B00000000 is shown for clarity, but zero in any number format is zero (hmmm something philosophical there?)
</p>
<pclass='vspace'></p><p>Consequently - to clear (set to zero) bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise AND operator (&=) with the constant B11111100
</p><pre>
1 0 1 0 1 0 1 0 variable
1 1 1 1 1 1 0 0 mask
----------------------
1 0 1 0 1 0 0 0
variable unchanged
bits cleared
</pre><p>Here is the same representation with the variable's bits replaced with the symbol x
</p>
<pclass='vspace'></p><pre>
x x x x x x x x variable
1 1 1 1 1 1 0 0 mask
----------------------
x x x x x x 0 0
variable unchanged
bits cleared
</pre><p>So if:
</p><pre>
myByte = 10101010;
myByte &= B1111100 == B10101000;
</pre>
<pclass='vspace'></p><h2>compound bitwise OR (|=)</h2>
<h4>Description </h4>
<p>The compound bitwise OR operator (|=) is often used with a variable and a constant to "set" (set to 1) particular bits in a variable.
</p>
<pclass='vspace'></p><h4>Syntax:</h4>
<pre>
x |= y; // equivalent to x = x | y;
</pre>
<pclass='vspace'></p><h4>Parameters</h4>
<p>x: a char, int or long variable<br/>y: an integer constant or char, int, or long
</p>
<pclass='vspace'></p><h4>Example:</h4>
<p>First, a review of the Bitwise OR (|) operator
</p><pre>
0 0 1 1 operand1
0 1 0 1 operand2
----------
0 1 1 1 (operand1 | operand2) - returned result
</pre><p>Bits that are "bitwise <spanclass='wikiword'>ORed</span>" with 0 are unchanged, so if myByte is a byte variable,<br/>
myByte | B00000000 = myByte;
</p>
<pclass='vspace'></p><p>Bits that are "bitwise <spanclass='wikiword'>ORed</span>" with 1 are set to 1 so:<br/>myByte & B11111111 = B11111111;
</p>
<pclass='vspace'></p><p>Consequently - to set bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise AND operator (&=) with the constant B00000011
</p><pre>
1 0 1 0 1 0 1 0 variable
0 0 0 0 0 0 1 1 mask
----------------------
1 0 1 0 1 0 1 1
variable unchanged
bits set
</pre><p>Here is the same representation with the variables bits replaced with the symbol x
<pclass='vspace'></p><p><em>Corrections, suggestions, and new documentation should be posted to the <aclass='urllink'href='http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?board=swbugs'rel='nofollow'>Forum</a>.</em>
</p>
<pclass='vspace'></p><p>The text of the Arduino reference is licensed under a
<aclass='urllink'href='http://creativecommons.org/licenses/by-sa/3.0/'rel='nofollow'>Creative Commons Attribution-ShareAlike 3.0 License</a>. Code samples in the reference are released into the public domain.