section 2.9: Bitwise Operators

page 48

The bitwise operators are definitely a bit (pardon the pun) more esoteric than the parts of C we've covered so far (and, indeed, than probably most of C). We won't concentrate on them, but they do come up all the time, so you should eventually learn enough about them to recognize what they do, even if you don't use them in any of your own programs for a while. You may skip this section for now, though.

To see what the bitwise operators are doing, it may help to convert to binary for a moment and look at what's happening to the individual bits. In the example on page 48, suppose that n is 052525, which is 21845 decimal, or 101010101010101 binary. Then n & 0177, in base 2 and base 8 (binary and octal) looks like

	  101010101010101     052525
	& 000000001111111   & 000177
	  ---------------     ------
	          1010101        125

In the second example, if SET_ON is 012 and x is 0, then x | SET_ON looks like

	        000000000     000000
	      | 000001010   | 000012
	        ---------     ------
	             1010         12
and if x starts out as 402, it looks like
	        100000010     000402
	      | 000001010   | 000012
	        ---------     ------
	        100001010        412
Note that with &, anywhere we have a 0 we turn bits off, and anywhere we have a 1 we copy bits through from the other side. With |, anywhere we have a 1 we turn bits on, and anywhere we have a 0 we leave bits alone.

You'll frequently see the word mask used, both as a noun and a verb. You can imagine that we've cut a mask or stencil out of cardboard, and are using spray paint to spray through the mask onto some other piece of paper. For |, the holes in the mask are like 1's, and the spray paint is like 1's, and we paint more 1's onto the underlying paper. (If there was already paint under a hole, nothing really changes if we get more paint on it; it's still a ``1''.)

The & operator is a bit harder to fit into this analogy: you can either imagine that the holes in the mask are 1's and you're spraying some preservative which will fix some of the underlying bits after which the others will get washed off, or you can imagine that the holes in the mask are 0's, and you're spraying some erasing paint or some background color which obliterates anything (i.e. any 1's, any foreground color) it reaches.

For a bit more information on ``bitwise'' operations, see the handout, ``A Brief Refresher on Some Math Often Used in Computing.''

page 49

Work through the example at the top of the page, and convince yourself that 1 & 2 is 0 and that 1 && 2 is 1.

The precedence of the bitwise operators is not what you might expect, and explicit parentheses are often needed, as noted in this deep sentence from page 52:

Note that the precedence of the bitwise operators &, ^, and | falls below == and !=. This implies that bit-testing expressions like
	if ((x & MASK) == 0) ...
must be fully parenthesized to give proper results.


Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995, 1996 // mail feedback