1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
<title>Arduino - Bitshift </title>
5
<link rel='stylesheet' href='arduino.css' type='text/css' />
6
<meta name="verify-v1" content="TtxFIEJAB6zdJ509wLxjnapQzKAMNm9u0Wj4ho6wxIY=" />
12
<div class="title"><a href="http://www.arduino.cc"/>Arduino</a></div>
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/">
6
23
<!-- SiteSearch Google -->
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>
32
<a class='wikilink' href='http://arduino.cc/en/Main/Software'>Download</a>
34
<a class='wikilink' href='Guide_index.html'>Getting Started</a>
36
<a class='wikilink' href='http://arduino.cc/en/Tutorial/HomePage'>Learning</a>
38
<a class='wikilink' href='index.html'>Reference</a>
40
<a class='wikilink' href='http://arduino.cc/en/Main/Hardware'>Hardware</a>
42
<a class='wikilink' href='FAQ.html'>FAQ</a>
44
<p class='vspace'></p>
47
<a class="urllink" href="http://www.arduino.cc/blog/" rel="nofollow">Blog »</a> |
48
<a class="urllink" href="http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl" rel="nofollow">Forum »</a> |
49
<a class="urllink" href="http://www.arduino.cc/playground/" rel="nofollow">Playground »</a>
55
<p><strong>Reference</strong> <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>
57
<p class='vspace'></p><h2>bitshift left (<<), bitshift right (>>)</h2>
59
<p>From <em>The Bitmath Tutorial</em> in The Playground
61
<p class='vspace'></p><p>There are two bit shift operators in C++: the left shift operator << and the right shift operator >>. These operators cause the bits in the left operand to be shifted left or right by the number of positions specified by the right operand.<br /><br />More on bitwise math may be found <a class='urllink' href='http://www.arduino.cc/playground/Code/BitMath' rel='nofollow'>here.</a>
63
<p class='vspace'></p><h4>Syntax</h4>
64
<p>variable << number_of_bits
66
<p class='vspace'></p><p>variable >> number_of_bits
68
<p class='vspace'></p><h4>Parameters</h4>
69
<p>variable - (byte, int, long)
70
number_of_bits integer <= 32
72
<p class='vspace'></p><h4>Example:</h4>
73
<pre> int a = 5; // binary: 0000000000000101
74
int b = a << 3; // binary: 0000000000101000, or 40 in decimal
75
int c = b >> 3; // binary: 0000000000000101, or back to 5 like we started with
77
<p class='vspace'></p><p>When you shift a value x by y bits (x << y), the leftmost y bits in x are lost, literally shifted out of existence:
79
<p class='vspace'></p><pre> int a = 5; // binary: 0000000000000101
80
int b = a << 14; // binary: 0100000000000000 - the first 1 in 101 was discarded
82
<p class='vspace'></p><p>If you are certain that none of the ones in a value are being shifted into oblivion, a simple way to think of the left-shift operator is that it multiplies the left operand by 2 raised to the right operand power. For example, to generate powers of 2, the following expressions can be employed:
84
<p class='vspace'></p><pre> 1 << 0 == 1
94
<p class='vspace'></p><p>When you shift x right by y bits (x >> y), and the highest bit in x is a 1, the behavior depends on the exact data type of x. If x is of type int, the highest bit is the sign bit, determining whether x is negative or not, as we have discussed above. In that case, the sign bit is copied into lower bits, for esoteric historical reasons:
96
<p class='vspace'></p><pre> int x = -16; // binary: 1111111111110000
97
int y = x >> 3; // binary: 1111111111111110
99
<p class='vspace'></p><p>This behavior, called sign extension, is often not the behavior you want. Instead, you may wish zeros to be shifted in from the left. It turns out that the right shift rules are different for unsigned int expressions, so you can use a typecast to suppress ones being copied from the left:
101
<p class='vspace'></p><pre> int x = -16; // binary: 1111111111110000
102
int y = (unsigned int)x >> 3; // binary: 0001111111111110
104
<p class='vspace'></p><p>If you are careful to avoid sign extension, you can use the right-shift operator >> as a way to divide by powers of 2. For example:
106
<p class='vspace'></p><pre> int x = 1000;
107
int y = x >> 3; // integer division of 1000 by 8, causing y = 125.
109
<p class='vspace'></p><p><a class='wikilink' href='index.html'>Reference Home</a>
111
<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>
113
<p class='vspace'></p><p>The text of the Arduino reference is licensed under a
114
<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.
120
<div id="pagefooter">
121
<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>
123
<!--/PageFooterFmt-->