Operators
(Chapter 3 of Schilit)
Object Oriented Programming BS (AI) II
Compiled By
Abdul Haseeb Shaikh
Operators
• Arithmetic
• Bitwise
• Relational
• Logical
sk1
Arithmetic
operators
• Operands to these
• operators must be
• numeric
Slide 3
sk1 sher khalil, 25/03/2021
Example
(arithmetic
with int and
double)
Unary Operator
Unary Minus (-)
NOT(!)
Increment(++) (pre &
post)
Decrement(--) (pre &
post)
Modulus • Floating
• Integer
Operator(%) • What happens when left side is smaller than right
side?
Take a floating
point number as
input, find its
remainder when
divided with 5
Compound
Assignment
Operators
var = <var> op <expression> Equal
to var op= <expression>;
Example (compound operator)
How integers are stored in memory by Java
and representation of sign
• In java integers are signed:
• Store negative as well as positive values
• To store negative numbers, use the concept of Two’s complement:
• Invert all the bits and add 1 to the result from LSB
Bitwise
Operators
Bitwise Logical Operators
&, |, ^, and ~
Bitwise
NOT(Complement)
~
Bitwise AND
&
Bitwise OR |
Bitwise XOR ^
a=0011
b=0110
a|b=0111
a=0011
b=0110
a&b=0010
a=0011
b=0110
a^b=0101
a=0011
b=0110
a&b=0010
LOGICAL BINARY SHIFTS
Binary Shift Move the binary number
to left or right
Left Shift: Right Shift:
Equivalent to Equivalent to
Multiply by 2 Divide by 2
Left Shift and Right Shift Demo
Bitwise Operator Compound Operator
Relational Operators (Boolean Outcome)
Boolean Logical Operators
Boolean Logical Operators
Short Circuit Logical Operator
• = Operator
Assignment
Operator
The ? Operator
• Also known as ternary(three way) operator
• expression1?expression2:expression3
The ?
Operator
Task
• Input salary
• Use Ternary Operator to check if
the salary is above 70000 output
managerial level, otherwise
output staff level
• You will only use conditional
ternary operator