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>Integer Representations (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Integer Representations (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Integer Representations (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="Integers-in-Depth.html" rel="up" title="Integers in Depth">
|
---|
33 | <link href="Maximum-and-Minimum-Values.html" rel="next" title="Maximum and Minimum Values">
|
---|
34 | <link href="Integers-in-Depth.html" rel="prev" title="Integers in Depth">
|
---|
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="Integer-Representations"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Maximum-and-Minimum-Values.html" accesskey="n" rel="next">Maximum and Minimum Values</a>, Up: <a href="Integers-in-Depth.html" accesskey="u" rel="up">Integers in Depth</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="Integer-Representations-1"></span><h3 class="section">27.1 Integer Representations</h3>
|
---|
64 |
|
---|
65 | <span id="index-integer-representations"></span>
|
---|
66 | <span id="index-representation-of-integers"></span>
|
---|
67 |
|
---|
68 | <p>Modern computers store integer values as binary (base-2) numbers that
|
---|
69 | occupy a single unit of storage, typically either as an 8-bit
|
---|
70 | <code>char</code>, a 16-bit <code>short int</code>, a 32-bit <code>int</code>, or
|
---|
71 | possibly, a 64-bit <code>long long int</code>. Whether a <code>long int</code> is
|
---|
72 | a 32-bit or a 64-bit value is system dependent.<a id="DOCF11" href="#FOOT11"><sup>11</sup></a>
|
---|
73 | </p>
|
---|
74 | <span id="index-CHAR_005fBIT"></span>
|
---|
75 | <p>The macro <code>CHAR_BIT</code>, defined in <samp>limits.h</samp>, gives the number
|
---|
76 | of bits in type <code>char</code>. On any real operating system, the value
|
---|
77 | is 8.
|
---|
78 | </p>
|
---|
79 | <p>The fixed sizes of numeric types necessarily limits their <em>range
|
---|
80 | of values</em>, and the particular encoding of integers decides what that
|
---|
81 | range is.
|
---|
82 | </p>
|
---|
83 | <span id="index-two_0027s_002dcomplement-representation"></span>
|
---|
84 | <p>For unsigned integers, the entire space is used to represent a
|
---|
85 | nonnegative value. Signed integers are stored using
|
---|
86 | <em>two’s-complement representation</em>: a signed integer with <var>n</var>
|
---|
87 | bits has a range from <em>-2<sup>(<var>n</var> - 1)</sup></em> to -1 to 0
|
---|
88 | to 1 to <em>+2<sup>(<var>n</var> - 1)</sup> - 1</em>, inclusive. The leftmost, or
|
---|
89 | high-order, bit is called the <em>sign bit</em>.
|
---|
90 | </p>
|
---|
91 |
|
---|
92 | <p>There is only one value that means zero, and the most negative number
|
---|
93 | lacks a positive counterpart. As a result, negating that number
|
---|
94 | causes overflow; in practice, its result is that number back again.
|
---|
95 | For example, a two’s-complement signed 8-bit integer can represent all
|
---|
96 | decimal numbers from -128 to +127. We will revisit that
|
---|
97 | peculiarity shortly.
|
---|
98 | </p>
|
---|
99 | <p>Decades ago, there were computers that didn’t use two’s-complement
|
---|
100 | representation for integers (see <a href="Integers-in-Depth.html">Integers in Depth</a>), but they are
|
---|
101 | long gone and not worth any effort to support.
|
---|
102 | </p>
|
---|
103 |
|
---|
104 | <p>When an arithmetic operation produces a value that is too big to
|
---|
105 | represent, the operation is said to <em>overflow</em>. In C, integer
|
---|
106 | overflow does not interrupt the control flow or signal an error.
|
---|
107 | What it does depends on signedness.
|
---|
108 | </p>
|
---|
109 | <p>For unsigned arithmetic, the result of an operation that overflows is
|
---|
110 | the <var>n</var> low-order bits of the correct value. If the correct value
|
---|
111 | is representable in <var>n</var> bits, that is always the result;
|
---|
112 | thus we often say that “integer arithmetic is exact,” omitting the
|
---|
113 | crucial qualifying phrase “as long as the exact result is
|
---|
114 | representable.”
|
---|
115 | </p>
|
---|
116 | <p>In principle, a C program should be written so that overflow never
|
---|
117 | occurs for signed integers, but in GNU C you can specify various ways
|
---|
118 | of handling such overflow (see <a href="Integer-Overflow.html">Integer Overflow</a>).
|
---|
119 | </p>
|
---|
120 | <p>Integer representations are best understood by looking at a table for
|
---|
121 | a tiny integer size; here are the possible values for an integer with
|
---|
122 | three bits:
|
---|
123 | </p>
|
---|
124 | <table>
|
---|
125 | <thead><tr><th width="25%">Unsigned</th><th width="25%">Signed</th><th width="25%">Bits</th><th width="25%">2s Complement</th></tr></thead>
|
---|
126 | <tr><td width="25%">0</td><td width="25%">0</td><td width="25%">000</td><td width="25%">000 (0)</td></tr>
|
---|
127 | <tr><td width="25%">1</td><td width="25%">1</td><td width="25%">001</td><td width="25%">111 (-1)</td></tr>
|
---|
128 | <tr><td width="25%">2</td><td width="25%">2</td><td width="25%">010</td><td width="25%">110 (-2)</td></tr>
|
---|
129 | <tr><td width="25%">3</td><td width="25%">3</td><td width="25%">011</td><td width="25%">101 (-3)</td></tr>
|
---|
130 | <tr><td width="25%">4</td><td width="25%">-4</td><td width="25%">100</td><td width="25%">100 (-4)</td></tr>
|
---|
131 | <tr><td width="25%">5</td><td width="25%">-3</td><td width="25%">101</td><td width="25%">011 (3)</td></tr>
|
---|
132 | <tr><td width="25%">6</td><td width="25%">-2</td><td width="25%">110</td><td width="25%">010 (2)</td></tr>
|
---|
133 | <tr><td width="25%">7</td><td width="25%">-1</td><td width="25%">111</td><td width="25%">001 (1)</td></tr>
|
---|
134 | </table>
|
---|
135 |
|
---|
136 | <p>The parenthesized decimal numbers in the last column represent the
|
---|
137 | signed meanings of the two’s-complement of the line’s value. Recall
|
---|
138 | that, in two’s-complement encoding, the high-order bit is 0 when
|
---|
139 | the number is nonnegative.
|
---|
140 | </p>
|
---|
141 | <p>We can now understand the peculiar behavior of negation of the
|
---|
142 | most negative two’s-complement integer: start with 0b100,
|
---|
143 | invert the bits to get 0b011, and add 1: we get
|
---|
144 | 0b100, the value we started with.
|
---|
145 | </p>
|
---|
146 | <p>We can also see overflow behavior in two’s-complement:
|
---|
147 | </p>
|
---|
148 | <div class="example">
|
---|
149 | <pre class="example">3 + 1 = 0b011 + 0b001 = 0b100 = (-4)
|
---|
150 | 3 + 2 = 0b011 + 0b010 = 0b101 = (-3)
|
---|
151 | 3 + 3 = 0b011 + 0b011 = 0b110 = (-2)
|
---|
152 | </pre></div>
|
---|
153 |
|
---|
154 | <p>A sum of two nonnegative signed values that overflows has a 1 in the
|
---|
155 | sign bit, so the exact positive result is truncated to a negative
|
---|
156 | value.
|
---|
157 | </p>
|
---|
158 |
|
---|
159 | <div class="footnote">
|
---|
160 | <hr>
|
---|
161 | <h4 class="footnotes-heading">Footnotes</h4>
|
---|
162 |
|
---|
163 | <h5><a id="FOOT11" href="#DOCF11">(11)</a></h3>
|
---|
164 | <p>In theory,
|
---|
165 | any of these types could have some other size, bit it’s not worth even
|
---|
166 | a minute to cater to that possibility. It never happens on
|
---|
167 | GNU/Linux.</p>
|
---|
168 | </div>
|
---|
169 | <hr>
|
---|
170 | <div class="header">
|
---|
171 | <p>
|
---|
172 | Next: <a href="Maximum-and-Minimum-Values.html" accesskey="n" rel="next">Maximum and Minimum Values</a>, Up: <a href="Integers-in-Depth.html" accesskey="u" rel="up">Integers in Depth</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>
|
---|
173 | </div>
|
---|
174 |
|
---|
175 |
|
---|
176 |
|
---|
177 | </body>
|
---|
178 | </html>
|
---|