* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / notes.accompany.ansi.c / sx5i.html
blobb088b30c2f9bc9692377d5a6224e9bdcb0059c1b
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995, 1996 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>section 2.9: Bitwise Operators</title>
10 <link href="sx5h.html" rev=precedes>
11 <link href="sx5j.html" rel=precedes>
12 <link href="sx5.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>section 2.9: Bitwise Operators</H2>
17 page 48
18 <p>The bitwise operators are definitely a bit
19 (pardon the pun)
20 more esoteric than the parts of C we've covered so far
21 (and, indeed, than probably most of C).
22 We won't concentrate on them,
23 but they do come up all the time,
24 so you should
25 eventually learn
26 enough about them to recognize what they do,
27 even if you don't use them in any of your own programs for a while.
28 You may skip this section for now, though.
29 </p><p>To see what the bitwise operators are doing,
30 it may help to convert to binary for a moment and look at
31 what's happening to the individual bits.
32 In the example on page 48, suppose that <TT>n</TT> is 052525,
33 which is 21845 decimal, or 101010101010101 binary.
34 Then <TT>n &amp; 0177</TT>,
35 in base 2 and base 8 (binary and octal)
36 looks like
37 <pre> 101010101010101 052525
38 &amp; 000000001111111 &amp; 000177
39 --------------- ------
40 1010101 125
41 </pre></p><p>In the second example,
42 if <TT>SET_ON</TT> is 012
43 and <TT>x</TT> is 0,
44 then <TT>x | SET_ON</TT> looks like
45 <pre> 000000000 000000
46 | 000001010 | 000012
47 --------- ------
48 1010 12
49 </pre>and if <TT>x</TT> starts out as 402,
50 it looks like
51 <pre> 100000010 000402
52 | 000001010 | 000012
53 --------- ------
54 100001010 412
55 </pre>Note that with <TT>&amp;</TT>,
56 anywhere we have a 0 we turn bits off,
57 and anywhere we have a 1
58 we copy bits through from the other side.
59 With <TT>|</TT>,
60 anywhere we have a 1 we turn bits on,
61 and anywhere we have a 0 we leave bits alone.
62 </p><p>You'll frequently see the word <dfn>mask</dfn> used,
63 both as a noun and a verb.
64 You can imagine that we've cut a mask or stencil out of cardboard,
65 and are using spray paint to spray through the mask
66 onto some other piece of paper.
67 For <TT>|</TT>, the holes in the mask are like 1's,
68 and the spray paint is like 1's,
69 and we paint more 1's onto the underlying paper.
70 (If there was already paint under a hole,
71 nothing really changes if we get more paint on it;
72 it's still a ``1''.)
73 </p><p>The <TT>&amp;</TT> operator is a bit harder to fit into this analogy:
74 you can either imagine that the holes in the mask are 1's
75 and you're spraying some preservative
76 which will fix some of the underlying bits
77 after which the others will get washed off,
78 or you can imagine that the holes in the mask are 0's,
79 and you're spraying some erasing paint or some background color
80 which obliterates anything
81 (i.e. any 1's, any foreground color)
82 it reaches.
83 </p><p>For a bit more information on ``bitwise'' operations,
84 see the handout,
85 ``A Brief Refresher on Some Math Often Used in Computing.''
86 </p><p>page 49
87 </p><p>Work through the example at the top of the page,
88 and convince yourself that <TT>1 &amp; 2</TT> is 0
89 and that <TT>1 &amp;&amp; 2</TT> is 1.
90 </p><p>The precedence of the bitwise operators is not what you might expect,
91 and explicit parentheses are often needed,
92 as noted in this deep sentence from page 52:
93 <blockquote>Note that the precedence of the bitwise operators
94 <TT>&amp;</TT>, <TT>^</TT>, and <TT>|</TT>
95 falls below <TT>==</TT> and <TT>!=</TT>.
96 This implies that bit-testing expressions like
97 <pre> if ((x &amp; MASK) == 0) ...
98 </pre>must be fully parenthesized to give proper results.
99 </blockquote></p><hr>
101 Read sequentially:
102 <a href="sx5h.html" rev=precedes>prev</a>
103 <a href="sx5j.html" rel=precedes>next</a>
104 <a href="sx5.html" rev=subdocument>up</a>
105 <a href="top.html">top</a>
106 </p>
108 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
109 // <a href="copyright.html">Copyright</a> 1995, 1996
110 // <a href="mailto:scs@eskimo.com">mail feedback</a>
111 </p>
112 </body>
113 </html>