Sams Teach Yourself C in 21 Days

(singke) #1
Decimal shift left by result
255 1 254
255 2 252
255 3 248
255 4 240
255 5 224
255 6 192
255 7 128
Decimal shift right by result
255 1 127
255 2 63
255 3 31
255 4 15
255 5 7
255 6 3
255 7 1

The Bitwise Logical Operators ....................................................................

Three bitwise logical operators are used to manipulate individual bits in an integer data
type, as shown in Table 20.1. These operators have names similar to the TRUE/FALSElogi-
cal operators you learned about in earlier chapters, but their operations differ.

TABLE20.1 The bitwise logical operators
Operator Action
& AND
| Inclusive OR
^ Exclusive OR

These are all binary operators, setting bits in the result to 1 or 0 depending on the bits in
the operands. They operate as follows:


  • BitwiseANDsets a bit in the result to 1 only if the corresponding bits in both
    operands are 1 ; otherwise, the bit is set to 0. The ANDoperator is used to turn off, or
    clear, one or more bits in a value.

  • Bitwise inclusive ORsets a bit in the result to 0 only if the corresponding bits in
    both operands are 0 ; otherwise, the bit is set to 1. The ORoperator is used to turn
    on, or set, one or more bits in a value.

  • Bitwise exclusive ORsets a bit in the result to 1 if the corresponding bits in the
    operands are different (if one is 1 and the other is 0 ); otherwise, the bit is set to 0.
    The following are examples of how these operators work:


584 Day 20

OUTPUT

32 448201x-CH20 8/13/02 11:16 AM Page 584

Free download pdf