Thursday, March 10, 2011

2.12 Precedence and Order of Evaluation





















  • C does not specified the order of operands of an operator are evaluated. (exception ?: etc)
  • C does not specified the order of function arguments are evaluated
  •  Function calls, nested assignment statements, and increment and decrement operators cause " side effects" - some variables are changed as a by-product of the evaluation of an expression
  • ie. a[i] = i++; ( subscript is the old or new value of i? Different compliers, generate different answers, depending on machine architecture) 

2.11 Conditional Expressions

  • Conditional expression written with ternary operator " ?:" ( expr1 ? expr2 : expr3 )
  • expr1 is evaluated, if expr1 is true then expr2 is evaluated, otherwise expr3 is evaluated (ie. z = (a > b) ? a : b;       /* z = max (a,b) */ )  

2.10 Assignment Operators and Expressions

  • Assignment Operators: += (ie. i = i + 2 can be written as i += 2)
  • Most binary operators have a corresponding assignment operator op = (op:  + - * / % << >> & ^ | )
  • expr1 op = expr2 equal to expr1 = (expr1) op (expr2) 

Wednesday, March 9, 2011

2.9 Bitwise Operators

  • Six operators for bit manipulation:












  • The operators can only be applied to integral operands - int, char, short and long (signed or unsigned)
  • bitwise AND operator is often used to mask off some set of bits
  • bitwise inclusive OR operator is used to turn bits on
  • bitwise exclusive OR operator set a one in each bit position where its operands have different bits, and zero where they are the same
  • Shift operator shift the bit position of operand and fill vacated bits with zero
  • Unary operator yields the one's complement of an integer (convert each 1-bit into 0-bit and vice versa)

2.8 Increment and Decrement Operators

  • Increment operators ++ add 1 to the operand while decrement operator -- subtract 1 from the operand
  • ++ and -- can be used as prefix or postfix operators, effect might be different depending on use cases
  • The expression ++n increment n before its value is used, while the expression n++ increment after its value is used
  • ++ and -- can only be applied to variables

2.7 Type Conversions

  • When an operator has operands of different types, they are converted to a common type according to a small number of rules
  • In general, the only automatic conversions are those that convert a "narrower" operand to a "wider" operand, without loss of information ( ie. integer to floating point) 

2.6 Relational and Logical Operators

  • Relational operators (same precedence): >, >=, <, <=
  • Equality operators (lower precedence than relational): = =, ! =
  • Relational operators have lower precedence than arithmetic
  • Logical operators (evaluated left to right): && , | |
  • Precedence of && is higher than ||
  • Logical operators' precedence is lower than equality and relational
  • Unary negation operator ! convert a non-zero operand into 0 and a zero operand into 1 (ie. !valid)