Programming Language II
CSE-215
Prof. Dr. Mohammad Abu Yousuf
yousuf@juniv.edu
1
Operators
2
Introduction
• Most of its operators can be divided into the following
four groups:
arithmetic,
bitwise,
relational, and
logical.
3
Arithmetic Operators
4
Arithmetic Operators
• The operands of the arithmetic operators must be of a
numeric type. You cannot use them on boolean types,
but you can use them on char types, since the char type
in Java is, essentially, a subset of int.
5
Relational Operators
6
The Bitwise Operators
• These operators act upon the individual bits of their
operands
7
The Bitwise Logical Operators
• The bitwise logical operators are &, |, ^, and ~.
8
Bitwise OR
Bitwise OR is a binary operator (operates on two operands).
It's denoted by |.
The | operator compares corresponding bits of two operands.
If either of the bits is 1, it gives 1. If not, it gives 0. For
example,
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise OR Operation of 12 and 25
00001100
| 00011001
________
00011101 = 29 (In decimal)
9
Example 1: Bitwise OR
10
Bitwise AND
Bitwise AND is a binary operator (operates on two operands).
It's denoted by &.
The & operator compares corresponding bits of two operands.
If both bits are 1, it gives 1. If either of the bits is not 1, it
gives 0.
For example,
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bit Operation of 12 and 25
00001100
& 00011001
________
00001000 = 8 (In decimal) 11
Example 2: Bitwise AND
12
Bitwise Complement
Bitwise complement is an unary operator (works on only one
operand). It is denoted by ~.
The ~ operator inverts the bit pattern. It makes every 0 to 1,
and every 1 to 0.
35 = 00100011 (In Binary)
Bitwise complement Operation of 35
~ 00100011
________
11011100 = 220 (In decimal)
13
Example 3: Bitwise Complement
Why are we getting output -36 instead of 220?
It's because the compiler is showing 2's complement of that
number; negative notation of the binary number.
14
For any integer n, 2's complement of n will be -(n+1).
The bitwise complement of 35 is 220 (in decimal). The 2's
complement of 220 is -36. Hence, the output is -36 instead of 220.
15
Bitwise XOR
Bitwise XOR is a binary operator (operates on two
operands). It's denoted by ^.
The ^ operator compares corresponding bits of two
operands. If corresponding bits are different, it gives 1. If
corresponding bits are same, it gives 0.
For example,
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise XOR Operation of 12 and 25
00001100
| 00011001
________
00010101 = 21 (In decimal)
16
Example 4: Bitwise XOR
17
left shift, right shift
A= 00111100
<< (left shift)
• Binary Left Shift Operator. The left operands value is moved
left by the number of bits specified by the right operand
Example: A << 2 will give 240 which is 1111 0000.
>> (right shift)
• Binary Right Shift Operator. The left operands value is
moved right by the number of bits specified by the right
operand.
Example: A >> 2 will give 15 which is 1111
18
zero fill right shift
• >>> (zero fill right shift)Shift right zero fill operator. The
left operands value is moved right by the number of bits
specified by the right operand and shifted values are
filled up with zeros.
Example: A >>>2 will give 15 which is 0000 1111
19
Signed Left Shift
The left shift operator << shifts a bit pattern to the left by
certain number of specified bits, and zero bits are shifted into
the low-order positions.
20
Example: Signed Left Shift
21
Signed Right Shift
The right shift operator >> shifts a bit pattern to
the right by certain number of specified bits.
If the number is a 2's complement signed number, the
sign bit is shifted into the high-order positions.
22
Example: Signed Right Shift
23
Unsigned Right Shift
The unsigned right shift operator << shifts
zero into the leftmost position.
Notice, how signed and
unsigned right shift works
differently for 2's
complement.
The 2's complement
of 2147483645 is 3.
24
25
Boolean Logical Operators
• The Boolean logical operators shown here operate only
on boolean operands. All of the binary logical operators
combine two boolean values to form a resultant
boolean value.
26
Boolean Logical Operators
• The logical Boolean operators, &, |, and ^, operate on
boolean values in the same way that they operate on
the bits of an integer.
27
Boolean Logical Operators
the string
representation
of a Java
boolean value is
one of the literal
values true or
false
28
Java AND Operator Example: Logical && and Bitwise &
• The logical && operator doesn't check second condition if
first condition is false. It checks second condition only if first
one is true.
• The bitwise & operator always checks both conditions
whether first condition is true or false.
1.class OperatorExample{
2.public static void main(String args[]){
3.int a=10;
4.int b=5;
5.int c=20;
6.System.out.println(a<b&&a++<c);//false && true = false
7.System.out.println(a);//10 because second condition is not checked
8.System.out.println(a<b&a++<c);//false && true = false
9.System.out.println(a);//11 because second condition is checked
10.}}
Output:
false
10
false 29
11
Java AND Operator Example: Logical && and Bitwise &
• This is very useful when the right-hand operand depends on
the value of the left one in order to function properly.
• For example, the following code fragment shows how you can
take advantage of short-circuit logical evaluation to be sure that
a division operation will be valid before evaluating it:
• Since the short-circuit form of AND (&&) is used, there is no
risk of causing a run-time exception when denom is zero. If
this line of code were written using the single & version of
AND, both sides would be evaluated, causing a run-time
exception when denom is zero. 30
Java OR Operator Example: Logical || and Bitwise |
• The logical || operator doesn't check second condition if first
condition is true. It checks second condition only if first one is
false.
• The bitwise | operator always checks both conditions whether
first condition is true or false.
1.class OperatorExample{
2.public static void main(String args[]){
3.int a=10;
4.int b=5;
5.int c=20;
6.System.out.println(a>b||a<c);//true || true = true
7.System.out.println(a>b|a<c);//true | true = true
8.//|| vs |
9.System.out.println(a>b||a++<c);//true || true = true
10.System.out.println(a);//10 because second condition is not checked
11.System.out.println(a>b|a++<c);//true | true = true
12.System.out.println(a);//11 because second condition is checked
13.}} 31
Java OR Operator Example: Logical || and Bitwise |
Output: Previous program
true
true
true
10
true
11
32
33
Conditional Operator ( ? : )
• Conditional operator is also known as the ternary
operator. This operator consists of three operands and is
used to evaluate Boolean expressions. The goal of the
operator is to decide which value should be assigned to
the variable. The operator is written as:
34
Conditional Operator ( ? : )
35
instance of Operator
• This operator is used only for object reference variables.
The operator checks whether the object is of a particular
type (class type or interface type). instanceof operator is
written as:
Output
36
Operator Precedence
37
Thank you
38