1 / 32

Chapter 3 - Operators

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 - a unary plus operator: +

hungate
Download Presentation

Chapter 3 - Operators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 - Operators • Arithmetic Operators • unary and binary • casting • Assignment • shorthand notation • Bitwise operators • Precedence and associativity

  2. Unary arithmetic operators • unary minus operator: - • invert sign of number -a • unary plus operator: + • serves no obvious purpose (leaves a number unchanged) +a

  3. Binary arithmetic operators • addition + a+b • subtraction - a-b • multiplication * a*b • division / a/b • modulus % a%b

  4. 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

  5. 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 4th century. • 0, 4, 8, ..., 92, 96, 104, 108, ..., 396, 400, 404, ..., 1896, 1904, 1908, ..., 1996, 2000, 2004, ...

  6. 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

  7. 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!

  8. 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 year\n",year); else printf("\n%d is not a leap year\n",year); return 0; }

  9. 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

  10. 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

  11. Floating point inaccuracies • roundoff error: 1.0/3.0 + 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.2e307*4.3e34 = infinity (double) • underflow: two numbers combined to make a number too small for data type: 2.2e-307*4.3e-34 = zero (double)

  12. Program 3.2 - floating point woes /* example #3.2 determine the result of floating point inaccuracies */ #include <stdio.h> intmain(intargc, char *argv[]) { float x1=1./3., x2, x3, y1=2.e33, y3=2.e33, y5; double y2=2.e300, y4, x4=1./3., x5, x6; x2=x3=x5=x6=1./3.; y5=1.-(x1+x2+x3); y4=1.-(x4+x5+x6); printf("Roundoff error? \t 0 = %g = %g \n",y4, y5);

  13. Program 3.2 continued y5=1./(y1*y2); printf("underflow error? \t 0.25e-333 = %g\n",y5); y5=y1*y2; printf("overflow error? \t 4.e333 = %g\n",y5); y5=1./(y1*y3); printf("underflow error? \t 0.25e-66 = %g\n",y5); y5=y1*y3; printf("overflow error? \t 4.e66 = %g\n",y5); return 0; }

  14. 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!

  15. Promotion hierarchy (high-to-low) • long double • double • float • unsigned long int • long int • unsigned int • int • short • char

  16. Promotion examples: • int width, length; • float radius, height; • double pi, area, volume; • area=width*length • area=pi*radius*radius • area=2*pi*height*radius • volume=height*width*length • volume=height*radius*radius*pi

  17. 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);

  18. 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,...)

  19. 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/ba=a%b a+=ba-=b a*=b a/=ba%=b

  20. 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 b=b-1 a=++b a=b+1 b=b+1 • don’t use twice in one expression! • ??? = b++/b++

  21. 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

  22. Truth tables

  23. Bitwise ‘and’ operator ‘&’ • a = 181 = 0xB5 b = 108 = 0x6C • c=a&b = 36 = 0x24 a 1011 0101 b 0110 1100 c 0010 0100 • usually, one argument is a mask, designed to recover certain bits.

  24. Bitwise ‘or’ operator ‘|’ • a = 181 = 0xB5 b = 108 = 0x6C • d=a|b = 253 = 0xFD a 1011 0101 b 0110 1100 d 1111 1101 • usually, one argument is a mask, designed to set certain bits.

  25. Bitwise ‘exclusive or’ operator ‘^’ • a = 181 = 0xB5 b = 108 = 0x6C • e=a^b = 217 = 0xD9 a 1011 0101 b 0110 1100 e 1101 1001 • usually, one argument is a mask, designed to flip certain bits.

  26. Bitwise ‘one’s complement’ ‘~’ • a = 181 = 0xB5 • f = ~a = 74 = 0x4A a 1011 0101 f 0100 1010 • inverts all bits.

  27. Bitwise ‘shift left’ operator ‘<<’ • a = 181 = 0xB5; i=3 • g = a << i = 168 = 0xA8 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

  28. Bitwise ‘shift right’ operator ‘>>’ • a = 181 = 0xB5; 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

  29. More shorthand assignments • a &= b a = a & b • a ^= b a = a ^ b • a |= b a = a | b • a <<= b a = a << b • a >>= b a = a >> b

  30. 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 = %d\n",a&b); printf(" a|b = %d\n",a|b); printf(" a^b = %d\n",b^=a); printf(" ~a = %d\n",(unsigned char)~a); i=3; printf("a<<i = %d\n",(unsigned char)(a<<i)); printf("a>>i = %d\n",a>>=i); return 0; }

  31. C precedence and associativity () [] left to right ! ~ ++ -- + - * & (type) sizeof right to left * / % left to right + - left to right << >> left to right < <= > >= left to right == != left to right & left to right ^ left to right | left to right && left to right || left to right = += -= *= /= %= &= ^= |= <<= >>= right to left , left to right

  32. Advanced operators Only three more operators to go... J but that will have to wait until later... L

More Related