~ubuntu-branches/ubuntu/precise/arduino/precise

« back to all changes in this revision

Viewing changes to reference/BitwiseAnd.html

  • Committer: Bazaar Package Importer
  • Author(s): Scott Howard
  • Date: 2010-04-13 22:32:24 UTC
  • Revision ID: james.westby@ubuntu.com-20100413223224-jduxnd0xxnkkda02
Tags: upstream-0018+dfsg
ImportĀ upstreamĀ versionĀ 0018+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
2
<html>
 
3
<head>
 
4
  <title>Arduino - BitwiseAnd </title>
 
5
  <link rel='stylesheet' href='arduino.css' type='text/css' />
 
6
  <meta name="verify-v1" content="TtxFIEJAB6zdJ509wLxjnapQzKAMNm9u0Wj4ho6wxIY=" />
 
7
</head>
 
8
<body>
 
9
<div id="page">
 
10
<!--PageHeaderFmt-->
 
11
<div id="pageheader">
 
12
  <div class="title"><a href="http://www.arduino.cc"/>Arduino</a></div>
 
13
  <div class="search">
 
14
    <!-- SiteSearch Google -->
0
15
   <FORM method=GET action="http://www.google.com/search">
1
16
   <input type=hidden name=ie value=UTF-8>
2
17
   <input type=hidden name=oe value=UTF-8>
3
18
   <INPUT TYPE=text name=q size=25 maxlength=255 value="">
4
19
   <INPUT type=submit name=btnG VALUE="search">
5
20
   <input type=hidden name=domains value="http://www.arduino.cc/">
 
21
    <input type=hidden name=sitesearch value="http://www.arduino.cc/">
 
22
    </FORM>
6
23
   <!-- SiteSearch Google -->
 
24
  </div>
 
25
</div>
 
26
<!--/PageHeaderFmt-->
 
27
<!--PageLeftFmt-->
 
28
<div id="pagenav" style="text-align: right">
 
29
  <div style="float: left;">
 
30
  <p><a class='wikilink' href='http://arduino.cc/en/Main/Buy'>Buy</a>
 
31
|
 
32
<a class='wikilink' href='http://arduino.cc/en/Main/Software'>Download</a>
 
33
|
 
34
<a class='wikilink' href='Guide_index.html'>Getting Started</a>
 
35
|
 
36
<a class='wikilink' href='http://arduino.cc/en/Tutorial/HomePage'>Learning</a>
 
37
|
 
38
<a class='wikilink' href='index.html'>Reference</a>
 
39
|
 
40
<a class='wikilink' href='http://arduino.cc/en/Main/Hardware'>Hardware</a>
 
41
|
 
42
<a class='wikilink' href='FAQ.html'>FAQ</a>
 
43
</p>
 
44
<p class='vspace'></p>
 
45
 
 
46
  </div>
 
47
  <a class="urllink" href="http://www.arduino.cc/blog/" rel="nofollow">Blog &raquo;</a> |
 
48
  <a class="urllink" href="http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl" rel="nofollow">Forum &raquo;</a> |
 
49
  <a class="urllink" href="http://www.arduino.cc/playground/" rel="nofollow">Playground &raquo;</a>
 
50
</div>
 
51
<!--/PageLeftFmt-->
 
52
<div id="pagetext">
 
53
<!--PageText-->
 
54
<div id='wikitext'>
 
55
<p><strong>Reference</strong> &nbsp;  <a class='wikilink' href='index.html'>Language</a> (<a class='wikilink' href='Extended.html'>extended</a>) | <a class='wikilink' href='Libraries.html'>Libraries</a> | <a class='wikilink' href='Comparison.html'>Comparison</a> | <a class='wikilink' href='Changes.html'>Changes</a>
 
56
</p>
 
57
<p class='vspace'></p><h2>Bitwise AND (&amp;), Bitwise OR (|),  Bitwise XOR (^)</h2>
 
58
<h4>Bitwise AND (&amp;)</h4>
 
59
<p>The bitwise operators perform their calculations at the bit level of variables. They help solve a wide range of common programming problems. Much of the material below is from an excellent tutorial on bitwise math wihch may be found <a class='urllink' href='http://www.arduino.cc/playground/Code/BitMath' rel='nofollow'>here.</a>
 
60
</p>
 
61
<p class='vspace'></p><h4>Description and Syntax</h4>
 
62
<p>Below are descriptions and syntax for all of the operators. Further details may be found in the referenced tutorial.
 
63
</p>
 
64
<p class='vspace'></p><h4>Bitwise AND (&amp;)</h4>
 
65
<p>The bitwise AND operator in C++ is a single ampersand, &amp;, used between two other integer expressions. Bitwise AND operates on each bit position of the surrounding expressions independently, according to this rule: if both input bits are 1, the resulting output is 1, otherwise the output is 0. Another way of expressing this is:
 
66
</p>
 
67
<p class='vspace'></p><pre>    0  0  1  1    operand1
 
68
    0  1  0  1    operand2
 
69
    ----------
 
70
    0  0  0  1    (operand1 &amp; operand2) - returned result
 
71
</pre>
 
72
<p class='vspace'></p><p>In Arduino, the type int is a 16-bit value, so using &amp; between two int expressions causes 16 simultaneous AND operations to occur. In a code fragment like:
 
73
</p>
 
74
<p class='vspace'></p><pre>    int a =  92;    // in binary: 0000000001011100
 
75
    int b = 101;    // in binary: 0000000001100101
 
76
    int c = a &amp; b;  // result:    0000000001000100, or 68 in decimal.
 
77
</pre>
 
78
<p class='vspace'></p><p>Each of the 16 bits in a and b are processed by using the bitwise AND, and all 16 resulting bits are stored in c, resulting in the value 01000100 in binary, which is 68 in decimal.
 
79
</p>
 
80
<p class='vspace'></p><p>One of the most common uses of bitwise AND is to select a particular bit (or bits) from an integer value, often called masking. See below for an example
 
81
</p>
 
82
<p class='vspace'></p><h4>Bitwise OR (|)</h4>
 
83
<p>The bitwise OR operator in C++ is the vertical bar symbol, |. Like the &amp; operator, | operates independently each bit in its two surrounding integer expressions, but what it does is different (of course). The bitwise OR of two bits is 1 if either or both of the input bits is 1, otherwise it is 0. In other words:
 
84
</p>
 
85
<p class='vspace'></p><pre>    0  0  1  1    operand1
 
86
    0  1  0  1    operand2
 
87
    ----------
 
88
    0  1  1  1    (operand1 | operand2) - returned result
 
89
</pre>
 
90
<p class='vspace'></p><p>Here is an example of the bitwise OR used in a snippet of C++ code:
 
91
</p>
 
92
<p class='vspace'></p><pre>    int a =  92;    // in binary: 0000000001011100
 
93
    int b = 101;    // in binary: 0000000001100101
 
94
    int c = a | b;  // result:    0000000001111101, or 125 in decimal.
 
95
</pre>
 
96
<p class='vspace'></p><h4>Example Program</h4>
 
97
<p>A common job for the bitwise AND and OR operators is what programmers call Read-Modify-Write on a port. On microcontrollers, a port is an 8 bit number that represents something about the condition of the pins. Writing to a port controls all of the pins at once. 
 
98
</p>
 
99
<p class='vspace'></p><p>PORTD is a built-in constant that refers to the output states of digital pins 0,1,2,3,4,5,6,7. If there is 1 in an bit position, then that pin is HIGH. (The pins already need to be set to outputs with the pinMode() command.) So if we write <code>PORTD = B00110001;</code> we have made pins 2,3 &amp; 7 HIGH. 
 
100
One slight hitch here is that we <em>may</em> also have changeed the state of Pins 0 &amp; 1, which are used by the Arduino for serial communications so we may have interfered with serial communication. 
 
101
</p>
 
102
<p class='vspace'></p><pre>     Our algorithm for the program is:
 
103
</pre><ul><li>Get PORTD and clear out only the bits corresponding to the pins we wish to control (with bitwise AND).
 
104
</li><li>Combine the modified PORTD value with the new value for the pins under control (with biwise OR).
 
105
</li></ul><p class='vspace'></p><pre>int i;     // counter variable
 
106
int j;
 
107
 
 
108
void setup(){
 
109
DDRD = DDRD | B11111100; // set direction bits for pins 2 to 7, leave 0 and 1 untouched (xx | 00 == xx)
 
110
// same as pinMode(pin, OUTPUT) for pins 2 to 7
 
111
Serial.begin(9600);
 
112
}
 
113
 
 
114
void loop(){
 
115
for (i=0; i&lt;64; i++){
 
116
 
 
117
PORTD = PORTD &amp; B00000011;  // clear out bits 2 - 7, leave pins 0 and 1 untouched (xx &amp; 11 == xx)
 
118
j = (i &lt;&lt; 2);               // shift variable up to pins 2 - 7 - to avoid pins 0 and 1
 
119
PORTD = PORTD | j;          // combine the port information with the new information for LED pins
 
120
Serial.println(PORTD, BIN); // debug to show masking
 
121
delay(100);
 
122
   }
 
123
}
 
124
</pre>
 
125
<p class='vspace'></p><h4>Bitwise XOR (^)</h4>
 
126
<p>There is a somewhat unusual operator in C++ called bitwise EXCLUSIVE OR, also known as bitwise XOR. (In English this is usually pronounced "eks-or".) The bitwise XOR operator is written using the caret symbol ^. This operator is very similar to the bitwise OR operator |, only it evaluates to 0 for a given bit position when both of the input bits for that position are 1:
 
127
</p>
 
128
<p class='vspace'></p><pre>    0  0  1  1    operand1
 
129
    0  1  0  1    operand2
 
130
    ----------
 
131
    0  1  1  0    (operand1 ^ operand2) - returned result
 
132
</pre>
 
133
<p class='vspace'></p><p>Another way to look at bitwise XOR is that each bit in the result is a 1 if the input bits are different, or 0 if they are the same.
 
134
</p>
 
135
<p class='vspace'></p><p>Here is a simple code example:
 
136
</p>
 
137
<p class='vspace'></p><pre>    int x = 12;     // binary: 1100
 
138
    int y = 10;     // binary: 1010
 
139
    int z = x ^ y;  // binary: 0110, or decimal 6
 
140
</pre>
 
141
<p class='vspace'></p><p>The ^ operator is often used to toggle (i.e. change from 0 to 1, or 1 to 0) some of the bits in an integer expression.  In a bitwise OR operation if there is a 1 in the mask bit, that bit is inverted; if there is a 0, the bit is not inverted and stays the same. Below is a program to blink digital pin 5.
 
142
</p>
 
143
<p class='vspace'></p><pre>
 
144
// Blink_Pin_5
 
145
// demo for Exclusive OR
 
146
void setup(){
 
147
DDRD = DDRD | B00100000; // set digital pin five as OUTPUT 
 
148
Serial.begin(9600);
 
149
}
 
150
 
 
151
void loop(){
 
152
PORTD = PORTD ^ B00100000;  // invert bit 5 (digital pin 5), leave others untouched
 
153
delay(100);
 
154
}
 
155
</pre>
 
156
<p class='vspace'></p><p>See Also
 
157
</p><ul><li><a class='wikilink' href='Boolean.html'>&amp;&amp;</a>(Boolean AND)
 
158
</li><li><a class='wikilink' href='Boolean.html'>||</a>(Boolean OR)
 
159
</li></ul><p class='vspace'></p><p><a class='wikilink' href='index.html'>Reference Home</a>
 
160
</p>
 
161
<p class='vspace'></p><p><em>Corrections, suggestions, and new documentation should be posted to the <a class='urllink' href='http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?board=swbugs' rel='nofollow'>Forum</a>.</em>
 
162
</p>
 
163
<p class='vspace'></p><p>The text of the Arduino reference is licensed under a
 
164
<a class='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.
 
165
</p>
 
166
</div>
 
167
 
 
168
</div>
 
169
<!--PageFooterFmt-->
 
170
<div id="pagefooter">
 
171
  <a href='#'>Edit Page</a> | <a href='#'>Page History</a> | <a href='#' target='_blank'>Printable View</a> | <a href='http://arduino.cc/en/Site/AllRecentChanges'>All Recent Site Changes</a>
 
172
</div>
 
173
<!--/PageFooterFmt-->
 
174
</div>
 
175
</body>
 
176
</html>