0% found this document useful (0 votes)
9 views32 pages

ENEB340 Week05 Operators

IOT Slides

Uploaded by

nsasikum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views32 pages

ENEB340 Week05 Operators

IOT Slides

Uploaded by

nsasikum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

ENEB 340 Week 5 - 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: +


– serves no obvious purpose (leaves a number
unchanged) +a
Binary arithmetic operators
Øaddition + a+b
Øsubtraction - a-b
Ømultiplication * a*b
Ødivision / a/b
Ømodulus % 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 5.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, ...
Program 5.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 5.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 5.1 - Leap year
/* example #5.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;
}
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 + 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)
Program 5.2 - floating point woes
/* example #5.2 determine the result of floating point
inaccuracies
Written by W. Lawson. First written Sept 28, 2019 */
#include <stdio.h>
int main(int argc, 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);
Program 5.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;
}
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-to-low)
Ølong double
Ødouble
Øfloat
Øunsigned long int
Ølong int
Øunsigned int
Øint
Øshort
Øchar
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
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 unary 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 b=b-1
a=++b a=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
x1 and x2 x2=0 x2=1
x1=0 0 0
x1=1 0 1

x1 or x2 x2=0 x2=1
x1=0 0 1
x1=1 1 1

x1 xor x2 x2=0 x2=1


x1=0 0 1
x1=1 1 0
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.
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.
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.
Bitwise ‘one’s complement’ ‘~’
Øa = 181 = 0xB5
Øf = ~a = 74 = 0x4A
a 1011 0101
f 0100 1010
Øinverts all bits.
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
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
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
Program 5.3 - bitwise operators
/* example #5.3 bitwise operation examples
Written by W. Lawson First written Sept 28, 2019 */
#include <stdio.h>
int main(void)
{
unsigned char a=181, b=108, i;
printf(“a=%d b=%d i=%d\n”,a,b,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);
printf("a<<i = %d\n",(unsigned char)(a<<i));
printf("a>>i = %d\n",a>>=i);
return 0;
}
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

You might also like