* X more docs for C
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes / sx14b.html
blob6dfe40d4a9da481a24e40e9999d19cd2ac5cce58
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
5 <html>
6 <head>
7 <link rev="owner" href="mailto:scs@eskimo.com">
8 <link rev="made" href="mailto:scs@eskimo.com">
9 <title>Operators</title>
10 <link href="sx14a.html" rev=precedes>
11 <link href="sx14c.html" rel=precedes>
12 <link href="sx14.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>Operators</H2>
17 <p>The <dfn>bitwise operators</dfn>
18 <TT>&amp;</TT>, <TT>|</TT>, <TT>^</TT>, and <TT>~</TT>
19 operate on integers thought of as binary numbers or strings of bits.
20 The <TT>&amp;</TT> operator is bitwise AND,
21 the <TT>|</TT> operator is bitwise OR,
22 the <TT>^</TT> operator is bitwise exclusive-OR (XOR),
23 and the <TT>~</TT> operator is a bitwise negation or complement.
24 (<TT>&amp;</TT>, <TT>|</TT>, and <TT>^</TT> are ``binary''
25 in that they take two operands;
26 <TT>~</TT> is unary.)
27 These operators let you work with the individual bits of a variable;
28 one common use is to treat an integer as a set of single-bit
29 <dfn>flags</dfn>.
30 You might define the 3rd
31 (2**2)
32 bit as the ``verbose'' flag
33 bit by defining
34 <pre>
35 #define VERBOSE 4
36 </pre>
37 Then you can ``turn the verbose bit on''
38 in an integer variable <TT>flags</TT>
39 by executing
40 <pre>
41 flags = flags | VERBOSE;
43 flags |= VERBOSE;
44 </pre>
45 and turn it off with
46 <pre>
47 flags = flags &amp; ~VERBOSE;
49 flags &amp;= ~VERBOSE;
50 </pre>
51 and test whether it's set with
52 <pre>
53 if(flags &amp; VERBOSE)
54 </pre>
55 </p><p>The left-shift and right-shift operators
56 <TT>&lt;&lt;</TT> and <TT>&gt;&gt;</TT>
57 let you shift an integer left or right by some number of bit positions;
58 for example,
59 <TT>value &lt;&lt; 2</TT>
60 shifts <TT>value</TT> left by two bits.
61 </p><p>The <TT>?:</TT> or <dfn>conditional</dfn> operator
62 (also called the ``ternary operator'')
63 essentially lets you embed an <TT>if</TT>/<TT>then</TT> statement
64 in an expression.
65 The assignment
66 <pre>
67 a = expr ? b : c;
68 </pre>
69 is roughly equivalent to
70 <pre>
71 if(expr)
72 a = b;
73 else a = c;
74 </pre>
75 Since you can use <TT>?:</TT> anywhere in an expression,
76 it can do things that <TT>if</TT>/<TT>then</TT> can't,
77 or that would be cumbersome with <TT>if</TT>/<TT>then</TT>.
78 For example, the function call
79 <pre>
80 f(a, b, c ? d : e);
81 </pre>
82 is roughly equivalent to
83 <pre>
84 if(c)
85 f(a, b, d);
86 else f(a, b, e);
87 </pre>
88 (Exercise: what would the call
89 <pre>
90 g(a, b, c ? d : e, h ? i : j, k);
91 </pre>
92 be equivalent to?)
93 </p><p>The comma operator lets you put two separate expressions where
94 one is required;
95 the expressions are executed one after the other.
96 The most common use for comma operators is when you want
97 multiple variables controlling a <TT>for</TT> loop,
98 for example:
99 <pre>
100 for(i = 0, j = 10; i &lt; j; i++, j--)
101 </pre>
102 </p><p>A <dfn>cast operator</dfn> allows you to explicitly force
103 conversion of a value from one type to another.
104 A cast consists of a type name in parentheses.
105 For example, you could convert an <TT>int</TT> to a <TT>double</TT>
106 by typing
107 <pre>
108 int i = 10;
109 double d;
110 d = (double)i;
111 </pre>
112 (In this case, though, the cast is redundant, since this is a
113 conversion that C would have performed for you automatically,
114 i.e. if you'd just said <TT>d = i</TT>
116 You use explicit casts in those circumstances
117 where C does not do a needed conversion automatically.
118 One example is division:
119 if you're dividing two integers and you want a floating-point result,
120 you must explicitly force at least one of the operands to floating-point,
121 otherwise C will perform an integer division and will discard the remainder.
122 The code
123 <pre>
124 int i = 1, j = 2;
125 double d = i / j;
126 </pre>
127 will set <TT>d</TT> to 0,
129 <pre>
130 d = (double)i / j;
131 </pre>
132 will set <TT>d</TT> to 0.5.
133 You can also ``cast to <TT>void</TT>''
134 to explicitly indicate that you're ignoring a function's return value,
135 as in
136 <pre>
137 (void)fclose(fp);
138 </pre>
140 <pre>
141 (void)printf("Hello, world!\n");
142 </pre>
143 (Usually, it's a bad idea to ignore return values,
144 but in some cases it's essentially inevitable,
145 and the <TT>(void)</TT> cast keeps some compilers from
146 issuing warnings every time you ignore a value.)
147 </p><p>There's a precise,
148 mildly elaborate
149 set of rules which C uses for converting values automatically,
150 in the absence of explicit casts.
151 </p><p>The <TT>.</TT> and <TT>-&gt;</TT> operators
152 let you access the members (components) of structures and unions.
153 </p><hr>
155 Read sequentially:
156 <a href="sx14a.html" rev=precedes>prev</a>
157 <a href="sx14c.html" rel=precedes>next</a>
158 <a href="sx14.html" rev=subdocument>up</a>
159 <a href="top.html">top</a>
160 </p>
162 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
163 // <a href="copyright.html">Copyright</a> 1995-1997
164 // <a href="mailto:scs@eskimo.com">mail feedback</a>
165 </p>
166 </body>
167 </html>