source: public/doc/gnu-c/Bitwise-Operations.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: 7.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>Bitwise Operations (GNU C Language Manual)</title>
23
24<meta name="description" content="Bitwise Operations (GNU C Language Manual)">
25<meta name="keywords" content="Bitwise Operations (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="Arithmetic.html" rel="up" title="Arithmetic">
33<link href="Assignment-Expressions.html" rel="next" title="Assignment Expressions">
34<link href="Shift-Hacks.html" rel="prev" title="Shift Hacks">
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="Bitwise-Operations"></span><div class="header">
59<p>
60Previous: <a href="Shift-Operations.html" accesskey="p" rel="prev">Shift Operations</a>, Up: <a href="Arithmetic.html" accesskey="u" rel="up">Arithmetic</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="Bitwise-Operations-1"></span><h3 class="section">6.8 Bitwise Operations</h3>
64<span id="index-bitwise-operators"></span>
65<span id="index-operators_002c-bitwise"></span>
66<span id="index-negation_002c-bitwise"></span>
67<span id="index-conjunction_002c-bitwise"></span>
68<span id="index-disjunction_002c-bitwise"></span>
69
70<p>Bitwise operators operate on integers, treating each bit independently.
71They are not allowed for floating-point types.
72</p>
73<p>The examples in this section use binary constants, starting with
74&lsquo;<samp>0b</samp>&rsquo; (see <a href="Integer-Constants.html">Integer Constants</a>). They stand for 32-bit integers
75of type <code>int</code>.
76</p>
77<dl compact="compact">
78<dt><code>~<code>a</code></code></dt>
79<dd><p>Unary operator for bitwise negation; this changes each bit of
80<code>a</code> from 1 to 0 or from 0 to 1.
81</p>
82<div class="example">
83<pre class="example">~0b10101000 &rArr; 0b11111111111111111111111101010111
84~0 &rArr; 0b11111111111111111111111111111111
85~0b11111111111111111111111111111111 &rArr; 0
86~ (-1) &rArr; 0
87</pre></div>
88
89<p>It is useful to remember that <code>~<var>x</var> + 1</code> equals
90<code>-<var>x</var></code>, for integers, and <code>~<var>x</var></code> equals
91<code>-<var>x</var> - 1</code>. The last example above shows this with -1
92as <var>x</var>.
93</p>
94</dd>
95<dt><code><code>a</code> &amp; <code>b</code></code></dt>
96<dd><p>Binary operator for bitwise &ldquo;and&rdquo; or &ldquo;conjunction.&rdquo; Each bit in
97the result is 1 if that bit is 1 in both <code>a</code> and <code>b</code>.
98</p>
99<div class="example">
100<pre class="example">0b10101010 &amp; 0b11001100 &rArr; 0b10001000
101</pre></div>
102
103</dd>
104<dt><code><code>a</code> | <code>b</code></code></dt>
105<dd><p>Binary operator for bitwise &ldquo;or&rdquo; (&ldquo;inclusive or&rdquo; or
106&ldquo;disjunction&rdquo;). Each bit in the result is 1 if that bit is 1 in
107either <code>a</code> or <code>b</code>.
108</p>
109<div class="example">
110<pre class="example">0b10101010 | 0b11001100 &rArr; 0b11101110
111</pre></div>
112
113</dd>
114<dt><code><code>a</code> ^ <code>b</code></code></dt>
115<dd><p>Binary operator for bitwise &ldquo;xor&rdquo; (&ldquo;exclusive or&rdquo;). Each bit in
116the result is 1 if that bit is 1 in exactly one of <code>a</code> and <code>b</code>.
117</p>
118<div class="example">
119<pre class="example">0b10101010 ^ 0b11001100 &rArr; 0b01100110
120</pre></div>
121</dd>
122</dl>
123
124<p>To understand the effect of these operators on signed integers, keep
125in mind that all modern computers use two&rsquo;s-complement representation
126(see <a href="Integer-Representations.html">Integer Representations</a>) for negative integers. This means
127that the highest bit of the number indicates the sign; it is 1 for a
128negative number and 0 for a positive number. In a negative number,
129the value in the other bits <em>increases</em> as the number gets closer
130to zero, so that <code>0b111<span class="roman">&hellip;</span>111</code> is -1 and
131<code>0b100<span class="roman">&hellip;</span>000</code> is the most negative possible integer.
132</p>
133<p><strong>Warning:</strong> C defines a precedence ordering for the bitwise
134binary operators, but you should never rely on it. You should
135never rely on how bitwise binary operators relate in precedence to the
136arithmetic and shift binary operators. Other programmers don&rsquo;t
137remember this precedence ordering, so always use parentheses to
138explicitly specify the nesting.
139</p>
140<p>For example, suppose <code>offset</code> is an integer that specifies
141the offset within shared memory of a table, except that its bottom few
142bits (<code>LOWBITS</code> says how many) are special flags. Here&rsquo;s
143how to get just that offset and add it to the base address.
144</p>
145<div class="example">
146<pre class="example">shared_mem_base + (offset &amp; (-1 &lt;&lt; LOWBITS))
147</pre></div>
148
149<p>Thanks to the outer set of parentheses, we don&rsquo;t need to know whether
150&lsquo;<samp>&amp;</samp>&rsquo; has higher precedence than &lsquo;<samp>+</samp>&rsquo;. Thanks to the inner
151set, we don&rsquo;t need to know whether &lsquo;<samp>&amp;</samp>&rsquo; has higher precedence than
152&lsquo;<samp>&lt;&lt;</samp>&rsquo;. But we can rely on all unary operators to have higher
153precedence than any binary operator, so we don&rsquo;t need parentheses
154around the left operand of &lsquo;<samp>&lt;&lt;</samp>&rsquo;.
155</p>
156<hr>
157<div class="header">
158<p>
159Previous: <a href="Shift-Operations.html" accesskey="p" rel="prev">Shift Operations</a>, Up: <a href="Arithmetic.html" accesskey="u" rel="up">Arithmetic</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>
160</div>
161
162
163
164</body>
165</html>
Note: See TracBrowser for help on using the repository browser.