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. -->
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
>
17 <p>The
<dfn>bitwise operators
</dfn>
18 <TT>&</TT>,
<TT>|
</TT>,
<TT>^
</TT>, and
<TT>~
</TT>
19 operate on integers thought of as binary numbers or strings of bits.
20 The
<TT>&</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>&</TT>,
<TT>|
</TT>, and
<TT>^
</TT> are ``binary''
25 in that they take two operands;
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
30 You might define the
3rd
32 bit as the ``verbose'' flag
37 Then you can ``turn the verbose bit on''
38 in an integer variable
<TT>flags
</TT>
41 flags = flags | VERBOSE;
47 flags = flags
& ~VERBOSE;
49 flags
&= ~VERBOSE;
51 and test whether it's set with
53 if(flags
& VERBOSE)
55 </p><p>The left-shift and right-shift operators
56 <TT><<</TT> and
<TT>>></TT>
57 let you shift an integer left or right by some number of bit positions;
59 <TT>value
<< 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
69 is roughly equivalent to
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
82 is roughly equivalent to
88 (Exercise: what would the call
90 g(a, b, c ? d : e, h ? i : j, k);
93 </p><p>The comma operator lets you put two separate expressions where
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,
100 for(i =
0, j =
10; i
< j; i++, j--)
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>
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.
127 will set
<TT>d
</TT> to
0,
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,
141 (void)printf(
"Hello, world!\n");
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,
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>-
></TT> operators
152 let you access the members (components) of structures and unions.
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>
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>