【文章內(nèi)容簡(jiǎn)介】
) ? Program produces the following output: 325 ANDed with 263 is 221 A First Book of ANSI C, Fourth Edition 17 The Inclusive OR Operator ? The result of an inclusive OR (|) bit parison is 1 if either bit is a 1。 otherwise the result is a 0 ? Inclusive OR operations are extremely useful in forcing selected bits to take on a 1 value or for passing through other bit values unchanged – For example, ? ORing with a 0 has the same effect as ANDing with a 1 A First Book of ANSI C, Fourth Edition 18 The Inclusive OR Operator (continued) ? Program produces the following output: 325 ORed with 263 is 367 A First Book of ANSI C, Fourth Edition 19 The Exclusive OR Operator ? The result of an exclusive OR (^) parison is 1 if one and only one of the bits being pared is a 1。 otherwise the result is 0 ? An exclusive OR operation can be used to create the opposite value, or plement, of any individual bit in a variable – For example, A First Book of ANSI C, Fourth Edition 20 The Complement Operator ? ~ is the unary plement operator – If op1 contains the binary number 11001010, ~op1 replaces this number with 00110101 ? ~ is used to force any bit in an operand to 0, independent of the number of bits used to store it – op1 amp。= ~07。 sets the last 3 bits of op1 to 0 – This statement is equivalent to the following: op1 = op1 amp。 0177770。 /* if an int has 16 bits */ op1 = op1 amp。 027777777770。 /* if it has 32 bits */ – This makes the program portable between machines using different integer storage sizes A First Book of ANSI C, Fourth Edition 21 DifferentSized Data Items ? When amp。, |, and ^ are used with operands of different sizes, the shorter operand is increased in bit size to match the size of the larger operand ? When extending signed numbers, the original leftmost bit is reproduced in the additional bits that are added to the number A First Book of ANSI C, Fourth Edition 22 DifferentSized Data Items (continued) A First Book of ANSI C, Fourth Edition 23 DifferentSized Data Items (continued) A First Book of ANSI C, Fourth Edition 24 The Shift Operators ? The left shift operator, , causes the bits in an operand to be shifted to the left by a given amount – For example, p1 = op1 4。 ? For unsigned integers, each left shift corresponds to multiplication by 2 – Also true for signed numbers using two’s plement representation, as long as the leftmost bit does not switch values A First Book of ANSI C, Fourth Edition 25 The Shift Operators (continued) A First Book of ANSI C, Fourth Edition 26 The Shift Operators (continued) ? The right shift operator, , causes the bits in an operand to be shifted to the right by a given amount