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

« back to all changes in this revision

Viewing changes to reference/Bitshift.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 - Bitshift </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>bitshift left (&lt;&lt;), bitshift right (&gt;&gt;)</h2>
 
58
<h4>Description</h4>
 
59
<p>From <em>The Bitmath Tutorial</em>  in The Playground
 
60
</p>
 
61
<p class='vspace'></p><p>There are two bit shift operators in C++: the left shift operator &lt;&lt; and the right shift operator &gt;&gt;. 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>
 
62
</p>
 
63
<p class='vspace'></p><h4>Syntax</h4>
 
64
<p>variable &lt;&lt; number_of_bits
 
65
</p>
 
66
<p class='vspace'></p><p>variable &gt;&gt; number_of_bits 
 
67
</p>
 
68
<p class='vspace'></p><h4>Parameters</h4>
 
69
<p>variable - (byte, int, long)
 
70
number_of_bits integer &lt;= 32
 
71
</p>
 
72
<p class='vspace'></p><h4>Example:</h4>
 
73
<pre>    int a = 5;        // binary: 0000000000000101
 
74
    int b = a &lt;&lt; 3;   // binary: 0000000000101000, or 40 in decimal
 
75
    int c = b &gt;&gt; 3;   // binary: 0000000000000101, or back to 5 like we started with
 
76
</pre>
 
77
<p class='vspace'></p><p>When you shift a value x by y bits (x &lt;&lt; y), the leftmost y bits in x are lost, literally shifted out of existence:
 
78
</p>
 
79
<p class='vspace'></p><pre>    int a = 5;        // binary: 0000000000000101
 
80
    int b = a &lt;&lt; 14;  // binary: 0100000000000000 - the first 1 in 101 was discarded
 
81
</pre>
 
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:
 
83
</p>
 
84
<p class='vspace'></p><pre>    1 &lt;&lt;  0  ==    1
 
85
    1 &lt;&lt;  1  ==    2
 
86
    1 &lt;&lt;  2  ==    4
 
87
    1 &lt;&lt;  3  ==    8
 
88
    ...
 
89
    1 &lt;&lt;  8  ==  256
 
90
    1 &lt;&lt;  9  ==  512
 
91
    1 &lt;&lt; 10  == 1024
 
92
    ...
 
93
</pre>
 
94
<p class='vspace'></p><p>When you shift x right by y bits (x &gt;&gt; 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:
 
95
</p>
 
96
<p class='vspace'></p><pre>    int x = -16;     // binary: 1111111111110000
 
97
    int y = x &gt;&gt; 3;  // binary: 1111111111111110
 
98
</pre>
 
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:
 
100
</p>
 
101
<p class='vspace'></p><pre>    int x = -16;                   // binary: 1111111111110000
 
102
    int y = (unsigned int)x &gt;&gt; 3;  // binary: 0001111111111110
 
103
</pre>
 
104
<p class='vspace'></p><p>If you are careful to avoid sign extension, you can use the right-shift operator &gt;&gt; as a way to divide by powers of 2. For example:
 
105
</p>
 
106
<p class='vspace'></p><pre>    int x = 1000;
 
107
    int y = x &gt;&gt; 3;   // integer division of 1000 by 8, causing y = 125.
 
108
</pre>
 
109
<p class='vspace'></p><p><a class='wikilink' href='index.html'>Reference Home</a>
 
110
</p>
 
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>
 
112
</p>
 
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.
 
115
</p>
 
116
</div>
 
117
 
 
118
</div>
 
119
<!--PageFooterFmt-->
 
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>
 
122
</div>
 
123
<!--/PageFooterFmt-->
 
124
</div>
 
125
</body>
 
126
</html>