Unformatted text preview:

1CMSC 212 – S05 (lect 6)Announcementsz Program #1B– Due week from Thursdayz Reading– Chapter 5 (today)– Chapter 15 (Thursday)2CMSC 212 – S05 (lect 6)Arithmetic Operators+ Add two numbers- Subtract two numbers* Multiply two numbers/ Divide two numbersFor integers, divide truncates to whole number% Remainder of integer divideAll except % may be used with integers or floats3CMSC 212 – S05 (lect 6)Bit Operationsz Numbers are represented by a fixed number of bits– typically int = 32 bits, char = 8 bits, long = 32/64 bitsz C permits direct manipulation of bits within a number– This is powerful: can get exactly what you want– This can be non-portable: easy to write programs that don't work on different types of computersz Numbers as a series of bits:1 1110 000left most bit right most bit4CMSC 212 – S05 (lect 6)Bit Shift Operatorz C has operators to bit shift numbers– number of bits changed depends on size of variablez Left Shift (number << xxx)– Move each bit of number to the left by xxx bit positions– Leftmost xxx bits discarded– Rightmost xxx bits gets 0z Right Shift (number >> xxx)– Move each bit of number to the right by xxx bit positions– Rightmost xxx bits discarded– Leftmost xxx bits gets 0 or replicate sign bit• for unsigned gets 0• for signed, its implementation dependent5CMSC 212 – S05 (lect 6)Examples of Bit Shiftunsigned int a, b;a = 0x0000 0010;b = a >> 1; /* b is now 0x0000 0008 */b = a >> 4; /* b is now 0x0000 0001 */b = a >> 5; /* b is now 0x0000 0000 */b = a << 1; /* b is now 0x0000 0020 */b = a << 4; /* b is now 0x0000 0100 */b = a << 5; /* b is now 0x0000 0200 */6CMSC 212 – S05 (lect 6)Bitwise Operatorsz And (&)– for each bit in two numbers, "and" the bits togetherz Or (|)– for each bit in two numbers, "or" the bits togetherz Xor (^)– for each bit in two numbers, "xor" the bits toegher01010010And01010111Or01010101Xor7CMSC 212 – S05 (lect 6)Assignment Operatorz Assignment is an operator, not a statement!– x = y + 3;– a = x = y + 3;– assignment is right associative, so• a = x = y + 3 is the same as a = (x = y + 3)– value of assignment operator is result of assignment• if truncation is applied, the truncated value is used• Consider:unsigned char x;unsigned int a, b;b = 0xabcd;a = x = b;at the end, a = 0xcd;8CMSC 212 – S05 (lect 6)Compound Assignmentz Shorthand to combine binary operator and assignment– += add right operand the left operand and update left• a += 3 is equivalent to a = a + 3– Also: -=, *=, /=, %=, <<=, >>=, &=, ^=, |=z Handy for complex expressions in LHS– a[ i * 2 + j - f(n)] = a[ i * 2 + j - f(n)] + 3;–a[i * 2 + j -f(n)] += 3;– assumes f(n) has no side effects9CMSC 212 – S05 (lect 6)Unary Operatorsz ! logical not operator– if the operand is true, result is false– if the operand is false, result is true– is really an integer operand• produces 0 for false • produces 1 for true–b = !(a == 3);z ~ bit-wise negation (ones complement)– flip each bit position –b = ~a;z - negation (twos complement)– changes sign of a number–a = 3;– b = -a; /* b is now -3 */10CMSC 212 – S05 (lect 6)Unary Operators (cont.)z & Return the address of a variable–int a, *b;–a = 3;– b = & a; /* b now points to the location of a */z sizeof - return number of bytes in variable or type–int a;– sizeof int– sizeof(int);– sizeof(a);z (<typeName>) - convert to a new type– int a; float b;– b = (float) a;11CMSC 212 – S05 (lect 6)Unary Operators (cont.)z ++ increment operator– can be prefix or postfix ++a - add one to a and use new value as result of expression.a++ - add one to a and use old value as result of expression.z -- decrement operator– can be prefix or postfix --a - subtract one from a and use new value as result of expressiona-- - subtract one from a and use old value as result of expression.Examples:a = --b + 10;c = ++a;d = b++;12CMSC 212 – S05 (lect 6)Relational Operatorsz Take two operands, result is integer– 0 for false, 1 for true– > >= < <= != ==z != is not equalz == is equal (notice difference from assignment)z Can use variable in conditional–int a;– if (a) { … is the same as if (a != 0) { …– if (!a) { … is the same as if (a == 0) {13CMSC 212 – S05 (lect 6)Logical Operatorsz && - and– if both operands are non-zero result is 1– else result is 0– uses short-circuit evaluation• if the first operand is not true, second is not evaluated• if ((a++) && (--b)) { .. }– if a is zero at start, a = 1 and b is not changedz || or – if either operands is non-zero result is 1– else result is 1z Caution: && is not the same as &– & performs a bitwise operation14CMSC 212 – S05 (lect 6)Other Operatorsz expr1 ? expr2 : expr3– if expr1 is non-zero, expr2 is evaluated and is the result– if expr1 is zero, expr3 is evaluated and is the result– a = (2 > 3) ? 65 : 0;z Comma– evaluates both operands, rightmost is the result– a = (1,2,3); vs. b = 1,2,3;• assignment is higher precedence than ,• a ends up 3 and b ends up 115CMSC 212 – S05 (lect 6)Type Conversionz Promotion of char and short– in expressions, char and short are promoted to intchar a, b, c;a = b + c;• b and c are converted to int, then the sum is truncatedz Arithmetic Conversions– can't be performed on different types• converted to "higher" typelong doubledoublefloatunsigned long intlong intunsigned intintHighest Type16CMSC 212 – S05 (lect 6)Precedencez There is a set of rules about precedence of operator– Most important precedence rule:• When in doubt, put in () to ensure correct order– Order (highest to lowest):•()• Function call, subscript, postfix increment/decrement• rest of unary operators• type conversion• Arithmetic operators• Relational operators• Bit operators• Assignment


View Full Document

UMD CMSC 212 - Lecture 6

Download Lecture 6
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 6 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 6 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?