C++ Primer Plus (P67) pps

20 159 0
C++ Primer Plus (P67) pps

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

->* Indirect member dereference 5 (all binary) *L–RMultiply / Divide ^ Modulus (remainder) 6 (all binary) +L–RAddition - Subtraction 7 <<L–RLeft shift >> Right shift 8 <L–RLess than <= Less than or equal to >= Greater than or equal to > Greater than 9 ==L–REqual to != Not equal to 10 (binary) &L–RBitwise AND 11 ^L–RBitwise XOR (exclusive OR) 12 |L–RBitwise OR 13 &&L–RLogical AND 14 ||L–RLogical OR 15 =R–LSimple assignment *= Multiply and assign /= Divide and assign %= Take remainder and assign += Add and assign -= Subtract and assign &= Bitwise AND and assign ^= Bitwise XOR and assign |= Bitwise OR and assign <<= Left shift and assign >>= Right shift and assign This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. 16 :?R–LConditional 17 throwL–RThrow exception 18 ,L–RCombine two expressions into one CONTENTS This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. CONTENTS Appendix E. OTHER OPERATORS Bitwise Operators Member Dereferencing Operators In order to avoid terminal obesity, the main text of this book doesn't cover two groups of operators. The first group consists of the bitwise operators, which let you manipulate individual bits in a value; these operators were inherited from C. The second group consists of two-member dereferencing operators; they are C++ additions. This appendix briefly summarizes these operators. Bitwise Operators The bitwise operators operate upon the bits of integer values. For example, the left-shift operator moves bits to the left, and the bitwise negation operator turns each one to a zero, and each zero to a one. Altogether, C++ has six such operators: <<, >>, ~, &, |, and ^. The Shift Operators The left-shift operator has the following syntax: value << shift Here value is the integer value to be shifted, and shift is the number of bits to shift. For example: 13 << 3 means shift all the bits in the value 13 three places to the left. The vacated places are filled with zeros, and bits shifted past the end are discarded (see Figure E.1). Figure E.1. The left-shift operator. This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. Because each bit position represents a value twice that of the bit to the right (see Appendix A, "Number Bases"), shifting one bit position is equivalent to multiplying the value by 2. Similarly, shifting two bit positions is equivalent to multiplying by 2[2], and shifting n positions is equivalent to multiplying by 2[n]. Thus, the value of 13 << 3 is 13 x 2[3], or 104. The left-shift operator provides a capability often found in assembly languages. However, an assembly left-shift operator directly alters the contents of a register, while the C++ operator produces a new value without altering existing values. For example, consider the following: int x = 20; int y = x << 3; This code doesn't change the value of x. The expression x << 3 uses the value of x to produce a new value, much as x + 3 produces a new value without altering x. If you want to use the left-shift operator to change the value of a variable, you also must use assignment. You can use regular assignment or the <<= operator, which combines shifting with assignment. x = x << 4; // regular assignment y <<= 2; // shift and assign The right-shift operator (>>), as you might expect, shifts bits to the right. It has the following syntax: This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. value >> shift Here value is the integer value to be shifted, and shift is the number of bits to shift. For example: 17 >> 2 means shift all the bits in the value 17 two places to the right. For unsigned integers, the vacated places are filled with zeros, and bits shifted past the end are discarded. For signed integers, vacated places may be filled with zeros or else with the value of the original leftmost bit. The choice depends upon the implementation (see Figure E.2 for an example illustrating filling with zeros). Figure E.2. Right-shift operator. Shifting one place to the right is equivalent to integer division by 2. In general, shifting n places to the right is equivalent to integer division by 2[n]. C++ also defines a right-shift-and-assign operator if you want to replace the value of a variable by the shifted value: int q = 43; q >>= 2; // replace 43 by 43 >> 2, or 10 This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. On some systems, using left- and right-shift operators may produce faster integer multiplication and division by 2 than using the division operator, but as compilers get better at optimizing code, such differences are fading. The Logical Bitwise Operators The logical bitwise operators are analogous to the regular logical operators, except they apply to a value on a bit-by-bit basis rather than to the whole. For example, consider the regular negation operator (!) and the bitwise negation (or complement) operator (~). The ! operator converts a true (or nonzero) value to false and a false value to true. The ~ operator converts each individual bit to its opposite (1 to 0 and 0 to 1). For example, consider the unsigned char value of 3: unsigned char x = 3; The expression !x has the value 0. To see the value of ~x, write it in binary form: 00000011. Then convert each 0 to 1 and each 1 to 0. This produces the value 11111100, or in base 10, the value 252 (see Figure E.3 for a 16-bit example). The new value is termed the complement of the original value. Figure E.3. Bitwise negation operator. The bitwise OR operator (|) combines two integer values to produce a new integer value. This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. Each bit in the new value is set to 1 if one or the other, or both, of the corresponding bits in the original values is set to 1. If both corresponding bits are 0, then the final bit is set to 0 (see Figure E.4). Figure E.4. The bitwise OR operator. Table E.1 summarizes how the | operator combines bits. Table E.1. Value of b1 | b2 Bit values b1 = 0 b1 = 1 b2 = 00 1 b2 = 11 1 The |= operator combines the bitwise OR operator with assignment: a |= b; // set a to a | b The bitwise XOR operator (^) combines two integer values to produce a new integer value. Each bit in the new value is set to 1 if one or the other, but not both, of the corresponding bits in the original values is set to 1. If both corresponding bits are 0 or both are 1, the final bit is set to 0 (see Figure E.5). This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. Figure E.5. The bitwise XOR operator. Table E.2 summarizes how the ^ operator combines bits. Table E.2. Value of b1 ^ b2 Bit values b1 = 0 b1 = 1 b2 = 00 1 b2 = 11 0 The ^= operator combines the bitwise XOR operator with assignment: a ^= b; // set a to a ^ b The bitwise AND operator (&) combines two integer values to produce a new integer value. Each bit in the new value is set to 1 only if both of the corresponding bits in the original values are set to 1. If either or both corresponding bits are 0, the final bit is set to 0 (see Figure E.6). Figure E.6. The bitwise AND operator. This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. Table E.3 summarizes how the & operator combines bits. Table E.3. Value of b1 & b2 Bit values b1 = 0 b1 = 1 b2 = 000 b2 = 101 The &= operator combines the bitwise AND operator with assignment: a &= b; // set a to a & b Alternative Representations C++ provides alternative representations of several bitwise operators, as shown in Table E.4. They are provided for locales that do not have the traditional bitwise operators as part of their character sets. This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. Table E.4. Bitwise Operator Representations Standard RepresentationAlternative Representation &bitand &=and_eq |bitor |=or_eq ~compl ^xor ^=xor_eq These alternative forms let you write statements like the following: b = compl a bitand b; // same as b = ~a & b; c = a xor b; // same as c = a ^ c; A Few Common Bitwise Techniques Often, controlling hardware involves turning particular bits on or off or checking their status. The bitwise operators provide the means to perform such actions. We'll go through the methods quickly. In the following examples, lottabits represents a general value, and bit represents the value corresponding to a particular bit. Bits are numbered from right to left, beginning with bit 0, so the value corresponding to bit position n is 2[n]. For example, an integer with only bit number 3 set to 1 has the value 2[3] or 8. In general, each individual bit corresponds to a power of 2, as described for binary numbers in Appendix A. So we'll use the term bit to represent a power of 2; this corresponds to a particular bit being set to 1 and all other bits set to 0. Turning a Bit On The following two operations each turn on the bit in lottabits corresponding to the bit represented by bit: This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. [...]... Because bit consists of one bit set to 1 and the rest set to 0, the value of lottabits & bit is either 0 (which tests as false) or bit, which being non-zero, tests as true Member Dereferencing Operators C++ lets you define pointers to members of a class These pointers involve special notations to declare them and to dereference them To see what's involved, let's start with a sample class: class Example... using the identifier inches in conjunction with a specific object (In a member function, you can omit the name of the object, but then the object is understood to be the one pointed to by the pointer.) C++ lets you define a member pointer to the identifier inches like this: int Example::*pt = &Example::inches; This pointer is a bit different from a regular pointer A regular pointer points to a specific . were inherited from C. The second group consists of two-member dereferencing operators; they are C++ additions. This appendix briefly summarizes these operators. Bitwise Operators The bitwise operators. and the bitwise negation operator turns each one to a zero, and each zero to a one. Altogether, C++ has six such operators: <<, >>, ~, &, |, and ^. The Shift Operators The left-shift. However, an assembly left-shift operator directly alters the contents of a register, while the C++ operator produces a new value without altering existing values. For example, consider the following: int

Ngày đăng: 07/07/2014, 06:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan