1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Shift Caveats (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Shift Caveats (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Shift Caveats (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Shift-Operations.html" rel="up" title="Shift Operations">
|
---|
33 | <link href="Shift-Hacks.html" rel="next" title="Shift Hacks">
|
---|
34 | <link href="Bits-Shifted-In.html" rel="prev" title="Bits Shifted In">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Shift-Caveats"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Shift-Hacks.html" accesskey="n" rel="next">Shift Hacks</a>, Previous: <a href="Bits-Shifted-In.html" accesskey="p" rel="prev">Bits Shifted In</a>, Up: <a href="Shift-Operations.html" accesskey="u" rel="up">Shift Operations</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Caveats-for-Shift-Operations"></span><h4 class="subsection">6.7.2 Caveats for Shift Operations</h4>
|
---|
64 |
|
---|
65 | <p><strong>Warning:</strong> If the shift count is greater than or equal to the
|
---|
66 | width in bits of the first operand, the results are machine-dependent.
|
---|
67 | Logically speaking, the “correct” value would be either -1 (for
|
---|
68 | right shift of a negative number) or 0 (in all other cases), but what
|
---|
69 | it really generates is whatever the machine’s shift instruction does in
|
---|
70 | that case. So unless you can prove that the second operand is not too
|
---|
71 | large, write code to check it at run time.
|
---|
72 | </p>
|
---|
73 | <p><strong>Warning:</strong> Never rely on how the shift operators relate in
|
---|
74 | precedence to other arithmetic binary operators. Programmers don’t
|
---|
75 | remember these precedences, and won’t understand the code. Always use
|
---|
76 | parentheses to explicitly specify the nesting, like this:
|
---|
77 | </p>
|
---|
78 | <div class="example">
|
---|
79 | <pre class="example">a + (b << 5) /* <span class="roman">Shift first, then add.</span> */
|
---|
80 | (a + b) << 5 /* <span class="roman">Add first, then shift.</span> */
|
---|
81 | </pre></div>
|
---|
82 |
|
---|
83 | <p>Note: according to the C standard, shifting of signed values isn’t
|
---|
84 | guaranteed to work properly when the value shifted is negative, or
|
---|
85 | becomes negative during the operation of shifting left. However, only
|
---|
86 | pedants have a reason to be concerned about this; only computers with
|
---|
87 | strange shift instructions could plausibly do this wrong. In GNU C,
|
---|
88 | the operation always works as expected,
|
---|
89 | </p>
|
---|
90 |
|
---|
91 |
|
---|
92 |
|
---|
93 | </body>
|
---|
94 | </html>
|
---|