Chapter 3 Operators Arithmetic Operators unary and binary
Chapter 3 - Operators Ø Arithmetic Operators – unary and binary – casting Ø Assignment – shorthand notation Ø Bitwise operators Ø Precedence and associativity
Unary arithmetic operators Ø unary minus operator: – invert sign of number Ø unary plus operator: -a + – serves no obvious purpose (leaves a number unchanged) +a
Binary arithmetic operators Ø addition Ø subtraction Ø multiplication Ø division Ø modulus + * / % a+b a-b a*b a/b a%b
The modulus (mod) operator Ø c=a%b Ø both arguments a and b must be integers Ø c is equal to the remainder of the integral division Ø Example of integer math: § 9/4 = 2 remainder 1, so with integer math, § 2 = 9/4 and 1 = 9%4
Program 3. 1 - definition Ø Determine if a given year is a leap year Ø What makes a leap year? § year is divisible by four, but not a century, except for every 4 th century. § 0, 4, 8, . . . , 92, 96, 104, 108, . . . , 396, 400, 404, . . . , 1896, 1904, 1908, . . . , 1996, 2000, 2004, . . .
Program 3. 1 - specification – start – declare variables – prompt for year – read in year – determine if leap year – if yes, print “year is a leap year” – if no, print “year is not a leap year” – stop
Program 3. 1 - stepwise refinement Ø determine if leap year § if • year % 400 == 0 • OR • year % 4 == 0 AND year % 100 != 0 § then it is a leap year!
Program 3. 1 - Leap year /* example #3. 1 determine if a given year is a leap year */ #include <stdio. h> int main(void) { int year; printf("input a year: t"); scanf(" %d", &year); if ((year%4 ==0 && year%100 != 0) || year%400 == 0 ) printf("n%d is a leap yearn", year); else printf("n%d is not a leap yearn", year); return 0; }
Precedence Ø order in which operators are evaluated: Ø Highest level: () [] Ø Next level: unary + Ø Next level: binary * / % Ø Next level: binary + Ø examples: 17 = 3+5*2+8/2 40 = (3+5)*(2+8)/2
Associativity Ø In C there is left-to-right associativity and right -to-left associativity Ø Unary + - go right to left Ø () [] * / % + - (binary) go left to right Ø examples: 8 = 8/-4/2*12/3*-2 32 = 8/(-4/2)*(12/3)*-2
Floating point inaccuracies Ø roundoff error: 1. 0/3. 0 + 1. 0/3. 0 may not equal 1 Ø overflow: two numbers combined to make a number too large for data type: 2. 2 e 307*4. 3 e 34 = infinity (double) Ø underflow: two numbers combined to make a number too small for data type: 2. 2 e-307*4. 3 e-34 = zero (double)
Program 3. 2 - floating point woes /* example #3. 2 determine the result of floating point inaccuracies */ #include <stdio. h> int main(int argc, char *argv[]) { float x 1=1. /3. , x 2, x 3, y 1=2. e 33, y 3=2. e 33, y 5; double y 2=2. e 300, y 4, x 4=1. /3. , x 5, x 6; x 2=x 3=x 5=x 6=1. /3. ; y 5=1. -(x 1+x 2+x 3); y 4=1. -(x 4+x 5+x 6); printf("Roundoff error? t 0 = %g n", y 4, y 5);
Program 3. 2 continued y 5=1. /(y 1*y 2); printf("underflow error? t 0. 25 e-333 = %gn", y 5); y 5=y 1*y 2; printf("overflow error? t 4. e 333 = %gn", y 5); y 5=1. /(y 1*y 3); printf("underflow error? t 0. 25 e-66 = %gn", y 5); y 5=y 1*y 3; printf("overflow error? t 4. e 66 = %gn", y 5); return 0; }
Implicit casting and promotion Ø If an equation contains more than a single data type, variables must be converted (cast) to the same type before the arithmetic operators can be evaluated (hardware rule). Ø This coercion is known as promotion because smaller data types are cast as larger data types. Ø This implicit casting is done only as needed!
Promotion hierarchy (high-tolow) Ølong double Øfloat Øunsigned long int Øunsigned int Øshort Øchar
Promotion examples: Ø int width, length; Ø float radius, height; Ø double pi, area, volume; – area=width*length – area=pi*radius – area=2*pi*height*radius – volume=height*width*length – volume=height*radius*pi
more promotion examples: Ø Which of the following are identically zero? float fahrenheit, celcius; celcius=(5/9)*(fahrenheit-32. ); celcius=(fahrenheit-32)*5/9; celcius= 5/9*(fahrenheit-32); celcius=(fahrenheit-32. )*(5/9); celcius= 5*(fahrenheit-32. )/9; celcius= 5/9. *(fahrenheit-32);
Explicit casting Ø There is a unitary operator that can be used to explicitly cast an operand: Ø general form: (type) expression Ø example: (double) temp – Note: only a copy of the data is made and recast you cannot change the declared data type. – Warning: casting to a “smaller” data type results in a loss in precision and perhaps worse! (overflow, undefined result, . . . )
Assignment operator = Ø has low precedence (this will be quantified later) Ø associates right to left Ø example a=b=c=d=3+4; Ø shorthand notation: a=a+b a=a-b a=a*b a=a/b a=a%b a+=b a-=b a*=b a/=b a%=b
Increment and decrement operators Ø a++ is shorthand for a=a+1 Ø a-- is shorthand for a=a-1 Ø you can increment/decrement before or after assignment: a=b-a=++b a=b+1 b=b-1 b=b+1 Ø don’t use twice in one expression! – ? ? ? = b++/b++
Bitwise operators Ø C has operators that manipulate the individual bits within a word and are useful in many applications: – digital-to-analog converters – control registers – error detection or just whenever computer memory is at a premium
Truth tables
Bitwise ‘and’ operator ‘&’ Ø a = 181 = 0 x. B 5 b = 108 = 0 x 6 C Ø c=a&b = 36 = 0 x 24 a 1011 0101 b 0110 1100 c 0010 0100 Ø usually, one argument is a mask, designed to recover certain bits.
Bitwise ‘or’ operator ‘|’ Ø a = 181 = 0 x. B 5 b = 108 = 0 x 6 C Ø d=a|b = 253 = 0 x. FD a 1011 0101 b 0110 1100 d 1111 1101 Ø usually, one argument is a mask, designed to set certain bits.
Bitwise ‘exclusive or’ operator ‘^’ Ø a = 181 = 0 x. B 5 b = 108 = 0 x 6 C Ø e=a^b = 217 = 0 x. D 9 a 1011 0101 b 0110 1100 e 1101 1001 Ø usually, one argument is a mask, designed to flip certain bits.
Bitwise ‘one’s complement’ ‘~’ Ø a = 181 = 0 x. B 5 Ø f = ~a = 74 = 0 x 4 A a 1011 0101 f 0100 1010 Ø inverts all bits.
Bitwise ‘shift left’ operator ‘<<’ Ø a = 181 = 0 x. B 5; i=3 Ø g = a << i = 168 = 0 x. A 8 a 1011 0101 g 1010 1000 Ø shifts ‘a’ i places to the left Ø Most significant bits (MSB) lost Ø Least significant bits (LSB) set to zero
Bitwise ‘shift right’ operator ‘>>’ Ø a = 181 = 0 x. B 5; i=3; h = a >>i =22, 246 a 1011 0101 h 0001 0110 (logical shift) h 1111 0110 (arithmetic shift) Ø shifts ‘a’ i places to the right Ø LSB lost, MSB compiler dependent – logical shift right: MSB set to zero – arithmetic shift right: MSB preserved
More shorthand assignments Ø a &= b Ø a ^= b Ø a |= b Ø a <<= b Ø a >>= b a=a&b a=a^b a=a|b a = a << b a = a >> b
Program 3. 3 - bitwise operators /* example #3. 3 bitwise operation examples */ #include <stdio. h> int main(void) { unsigned char a=181, b=108, i; printf(" a&b = %dn", a&b); printf(" a|b = %dn", a|b); printf(" a^b = %dn", b^=a); printf(" ~a = %dn", (unsigned char)~a); i=3; printf("a<<i = %dn", (unsigned char)(a<<i)); printf("a>>i = %dn", a>>=i); return 0; }
C precedence and associativityleft to right () [] ! ~ ++ -* / + << >> < <= == != & ^ | && || = += -= *= , + - * & (type) sizeof % > >= /= %= &= ^= |= <<= >>= right to left to right left to right left to right to left to right
Advanced operators Only three more operators to go. . . J but that will have to wait until later. . . L
- Slides: 32