source: public/doc/gnu-c/Integer-Representations.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 8.4 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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
69occupy 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
71possibly, a 64-bit <code>long long int</code>. Whether a <code>long int</code> is
72a 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
76of bits in type <code>char</code>. On any real operating system, the value
77is 8.
78</p>
79<p>The fixed sizes of numeric types necessarily limits their <em>range
80of values</em>, and the particular encoding of integers decides what that
81range 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
85nonnegative value. Signed integers are stored using
86<em>two&rsquo;s-complement representation</em>: a signed integer with <var>n</var>
87bits has a range from <em>-2<sup>(<var>n</var> - 1)</sup></em> to -1 to 0
88to 1 to <em>+2<sup>(<var>n</var> - 1)</sup> - 1</em>, inclusive. The leftmost, or
89high-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
93lacks a positive counterpart. As a result, negating that number
94causes overflow; in practice, its result is that number back again.
95For example, a two&rsquo;s-complement signed 8-bit integer can represent all
96decimal numbers from -128 to +127. We will revisit that
97peculiarity shortly.
98</p>
99<p>Decades ago, there were computers that didn&rsquo;t use two&rsquo;s-complement
100representation for integers (see <a href="Integers-in-Depth.html">Integers in Depth</a>), but they are
101long 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
105represent, the operation is said to <em>overflow</em>. In C, integer
106overflow does not interrupt the control flow or signal an error.
107What it does depends on signedness.
108</p>
109<p>For unsigned arithmetic, the result of an operation that overflows is
110the <var>n</var> low-order bits of the correct value. If the correct value
111is representable in <var>n</var> bits, that is always the result;
112thus we often say that &ldquo;integer arithmetic is exact,&rdquo; omitting the
113crucial qualifying phrase &ldquo;as long as the exact result is
114representable.&rdquo;
115</p>
116<p>In principle, a C program should be written so that overflow never
117occurs for signed integers, but in GNU C you can specify various ways
118of 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
121a tiny integer size; here are the possible values for an integer with
122three 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
137signed meanings of the two&rsquo;s-complement of the line&rsquo;s value. Recall
138that, in two&rsquo;s-complement encoding, the high-order bit is 0 when
139the number is nonnegative.
140</p>
141<p>We can now understand the peculiar behavior of negation of the
142most negative two&rsquo;s-complement integer: start with 0b100,
143invert the bits to get 0b011, and add 1: we get
1440b100, the value we started with.
145</p>
146<p>We can also see overflow behavior in two&rsquo;s-complement:
147</p>
148<div class="example">
149<pre class="example">3 + 1 = 0b011 + 0b001 = 0b100 = (-4)
1503 + 2 = 0b011 + 0b010 = 0b101 = (-3)
1513 + 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
155sign bit, so the exact positive result is truncated to a negative
156value.
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,
165any of these types could have some other size, bit it&rsquo;s not worth even
166a minute to cater to that possibility. It never happens on
167GNU/Linux.</p>
168</div>
169<hr>
170<div class="header">
171<p>
172Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.