Download Arithmetic Operators - Introduction to Low Level Programming | CMSC 212 and more Study notes Computer Science in PDF only on Docsity!
Announcements
z^
Program #1B– Due week from Thursday
z^
Reading– Chapter 5 (today)– Chapter 15 (Thursday)
Arithmetic Operators
+^
Add two numbers
-^
Subtract two numbers
*^
Multiply two numbers
/^
Divide two numbers
For integers, divide truncates to whole number
% Remainder of integer divideAll except % may be used with integers or floats
Bit Shift Operator
z^
C has operators to bit shift numbers– number of bits changed depends on size of variable
z^
Left Shift (number << xxx)– Move each bit of number to the left by xxx bit positions– Leftmost xxx bits discarded– Rightmost xxx bits gets 0
z^
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 dependent
Examples of Bit Shift
unsigned 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 */
Assignment Operator
z^
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;
Compound Assignment
z^
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 effects
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^
() - convert to a new type– int a; float b;– b = (float) a;
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++;
Logical Operators
z^
&& - 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 changed
z^
|| or– if either operands is non-zero result is 1– else result is 1
z^
Caution: && is not the same as &– & performs a bitwise operation
Other Operators
z^
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 1
Precedence
z^
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 operators• ,