Download presentation
Presentation is loading. Please wait.
Published byAllison Foster Modified over 8 years ago
1
CHAPTER 4 CS 3370 – C++ Expressions
2
Operators Unary -, *, ++, -- higher precedence than binary operators most associate right-to-left Binary most associate left-to-right exception: assignment Ternary Conditional operator: a ? b : c yields a value
3
Precedence and Associativity Associativity applies to operators of the same precedence left vs. right associativity Full list on page 166
4
lvalues and rvalues Term suggests components of an assignment statement lvalue = address (stored data and functions have addresses) rvalue = simple values (e.g., temporaries like x + y) x = y; y’s rvalue is copied to the location indicated by x’s lvalue driven by context rvalue lvalue
5
Unmodifiable lvalues const int n = 7; n has an address, therefore it has an lvalue but it cannot be modified lvalues are required: with &x, *x, and x[i] // x must be an lvalue modifiable lvalues are required: on the left side of an assignment as the target of -- and ++
6
Order of Evaluation Applies to the operands of binary operators it is usually unspecified in C++ int i = f1() * f2(); We don’t know which function will execute first don’t depend on the order you write them in use separate, sequential statements if order matters Undefined behavior: cout << i << ++i; // indeterminate result
7
More About ++ (and --) Unary operators Post is higher precedence than pre Pre returns an lvalue ++ ++x is legal Post return an rvalue x++ ++ is not legal ++x++ is not legal Consider x = *p++; ++ executes first (!!!) but it yields the un-incremented value then * executes after the assignment, p is incremented as a side effect
8
Sequence Points Points in code where pending side effects are carried out The + operator is not a sequence point: that’s why f1() + f2() was indeterminate Occur with the following operators/contexts: Logical connectives ( &&, || ) if (x++ && x > y) … Comma operator if (f1(), f()) … The ? in the ternary operator (?:) Statements Complete conditions in if, while, for, and switch End of an initializer: int a = x++, b = ++x; // done in order
9
Bitwise Operators
10
Who cares about bits? Most people don’t! Operating systems do Embedded systems programming Flags, etc. People short on memory resources do, Can pack boolean values into an int Examples: std::bitset You do! Since Program 3 requires bitwise operations
11
Bitwise Operators Bitwise-OR (|) Bitwise-AND (&) Bitwise-XOR (^)(“exclusive-or”) Bitwise-NOT(~) Shift-left (<<) Shift-right(>>) Only work on integers Any size (char, short, int, long) Unsigned integers should be used To avoid sign extension with >> and integral promotion
12
Typical Bit-processing Functions Test, set, reset, or flip (toggle) bits Extract a subset of bits as a number
13
Bitwise Principles ANDing a bit with a 1 reads it (b & 1 == b) ORing a bit with 1 sets it to 1 (b | 1 == 1) ANDing a bit with 0 resets it (b & 0 == 0) XORing a bit with 1 flips it (b ^ 1 == ~b) ORing a bit with 0 is a nop (b | 0 == b) ANDing a bit with 1 is a nop (b & 1 == b) XORing a bit with 0 is a nop (b ^ 0 == b)
14
Testing a Bit Use the & operator First form a one-mask Place 1 in the desired bit position (n), zeroes elsewhere: unsigned int mask = 1u << n; Note: bit-0 is on the far right (1’s place) Then AND it with the number x & mask The result is non-zero iff the bit is 1 To convert the result to 1 or 0: !!(x & mask) See printbits.cpp
15
Setting a Bit Use the |= operator OR the 1-mask into the number x |= mask
16
Setting Multiple Contiguous bits OR multiple 1’s into the number To set all bits: x = -1, or x = ~0u (preferred) Mask to set the lower n bits: mask = (1u << n) – 1 (preferred), or mask = ~0u >> (NBITS – n) where NBITS is # of bits in an int (= sizeof(int) * CHAR_BIT) Mask to set bits m through n (inclusive, m < n): mask = (1u << (n-m+1) – 1) << m
17
Resetting a Bit Use the &= operator Form a 0-mask 0 in the bit position, 1’s elsewhere By flipping the 1-mask: unsigned mask = ~(1u << n) Then AND into the number: x &= mask
18
Resetting Multiple Bits AND multiple 0’s into the number Flip the corresponding masks for setting bits forming zero-masks Then AND the mask (&=) into the number
19
Copying a Bit If the desired value is 0, reset the bit Otherwise set it No short-cut!
20
Flipping Bits XOR 1’s into the number Form the appropriate 1-mask x ^= mask
21
Extracting Contiguous Bits as a Number Form the 1-mask to extract the bits By AND-ing Shift right the appropriate number of positions so the right-most bit of interest is in the 1’s place See next slide Extracts components of an IEEE float
22
float x = 6.5; unsigned int n = *reinterpret_cast (&x); cout << hex << n << endl; // 40d00000 int sb = n >> 31; cout << sb << endl; // 0 unsigned int expmask = ((1u << 8) - 1) << 23; cout << expmask << endl; // 7f800000 int exp = (n & expmask) >> 23; cout << dec << exp << endl; // 129 cout > 24) << endl; // 129 (another way) int mask4 = 1u << 4; n |= mask4; // Set bit 4 cout << hex << n << endl; // 40d00010 n &= ~mask4; // Clear it cout << hex << n << endl; // 40d00000
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.