0% found this document useful (0 votes)
2 views

theory4

The document provides an overview of various types of operators in programming, including arithmetic, bitwise, relational, and logical operators. It explains concepts such as unary operators, compound assignment operators, and the ternary operator, along with examples and their applications. Additionally, it discusses how integers are stored in memory in Java using Two's complement for negative values.

Uploaded by

hassanmemonexe2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

theory4

The document provides an overview of various types of operators in programming, including arithmetic, bitwise, relational, and logical operators. It explains concepts such as unary operators, compound assignment operators, and the ternary operator, along with examples and their applications. Additionally, it discusses how integers are stored in memory in Java using Two's complement for negative values.

Uploaded by

hassanmemonexe2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

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

You might also like