Bitwise Operators

Download Report

Transcript Bitwise Operators

Java Operators
Operators
 Arithmetic Operators
 Relational Operators
 Bitwise Operators
 Logical Operators
 Assignment Operators
 Conditional Operators
Operators
Arithmetic Operators
 Arithmetic operators are used in mathematical expressions in
the same way that they are used in algebra.
 A = 10 and B = 20
 + ( Addition ) Ex. A + B will give 30
 - ( Subtraction ) Ex. A - B will give -10
 * ( Multiplication ) Ex. A * B will give 200
 / (Division) Ex. B / A will give 2
 % (Modulus) Ex. B % A will give 0
 ++ (Increment) Ex. B++ gives 21
 -- ( Decrement ) Ex. B-- gives 19
Increment and decrement
operators
public class Fruit {
public static void main(String[] args)
{
// Declare and initialize three variables
int numOranges = 5;
int numApples = 10;
int numFruit = 0;
// Increment oranges and calculate the total fruit
numFruit = ++numOranges + numApples;
System.out.println(“A totally fruity program”);
// Display the result
System.out.println(“Value of oranges is “ + numOranges);
System.out.println(“Total fruit is “ + numFruit);
}
}
Relational Operators
 A = 10 and B = 20
 == (equal to)
Ex: (A == B) is not true
 != (not equal to)
Ex: (A != B) is true.
 3 > (greater than)
Ex: (A > B) is not true.
 < (less than)
Ex : (A < B) is true.
 >= (greater than or equal to)
 <= (less than or equal to)
Ex. (A >= B) is not true
Ex. (A<= B) is true.
Bitwise Operators
Bitwise operator works on bits and performs bit-by-bit
operation. Assume if a = 60; and b = 13;
a = 0011 1100
b = 0000 1101
 & (bitwise and)
a&b = 0000 1100
 | (bitwise or)
a|b = 0011 1101
Test with Program
Bitwise Operators
 << (left shift)
A= 0011 1100
A << 2 will give 240 which is 1111 0000
>> (right shift)
A >> 2 will give 15 which is 1111
Test with Program
Logical Operators
 Boolean A = true, B = false
 && (logical and)
(A && B) is false.
 || (logical or)
(A || B) is true.
 ! (logical not)
!(A && B) is true
Test with Program
Assignment Operators
 =
C = A + B will assign value of A + B into C
 +=
C += A is equivalent to C = C + A
 -=
C -= A is equivalent to C = C - A
 *=
C *= A is equivalent to C = C * A
Assignment Operators
 /=
C /= A is equivalent to C = C / A
 %=
C %= A is equivalent to C = C % A
 <<=
C <<= 2 is same as C = C << 2
Bitwise Operators Example
public class BitwiseOperators {
public static void main(String args[])
int x = 60;
int y = 13;
int z;
System.out.println("x & y : " + (x & y));
System.out.println("x | y : " + (x | y));
System.out.println("x << 2: " + (x << 2));
System.out.println("x >> 2: " + (x >> 2));
}
Unary Operators Example
class UnaryDemo {
public static void main(String[] args) {
int result = +1;
// result is now 1
System.out.println(result);
result--;
// result is now 0
System.out.println(result);
result++;
// result is now 1
System.out.println(result);
result = -result;
// result is now -1
System.out.println(result);
boolean success = false;
// false
System.out.println(success);
// true
System.out.println(!success);
}
}
Unary Operators Example
class PrePostDemo {
public static void main(String[] args){
int i = 3;
i++;
// prints 4
System.out.println(i);
++i;
// prints 5
System.out.println(i);
// prints 6
System.out.println(++i);
// prints 6
System.out.println(i++);
// prints 7
System.out.println(i);
}
}
Logical Operators Example
class ConditionalDemo1 {
public static void main(String[] args){
int value1 = 1;
int value2 = 2;
if((value1 == 1) && (value2 == 2))
System.out.println("value1 is 1 AND value2 is 2");
if((value1 == 1) || (value2 == 1))
System.out.println("value1 is 1 OR value2 is 1");
}
}
Conditional Operator ( ? : )
Conditional operator is also known as the ternary operator
 x = (expression) ? value if true : value if false
public class CondOp {
public static void main(String args[]){
int a, b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );
}
}
Precedence of Operators
Operators
Precedence
postfix
() [] . (dot operator)
Left to right
unary
++ - -
Right to left
multiplicative
*
additive
+
shift
<< >> >>>
relational
<>
<=
equality
==
!=
bitwise AND
&
left-to-right
bitwise exclusive OR
^
left-to-right
bitwise inclusive OR
|
left-to-right
logical AND
&&
left-to-right
logical OR
||
left-to-right
ternary
?:
right-to-left
assignment
= +=
>>>=
/
! ~
%
-
-=
>=
instanceof
*= /= %= &= ^= |= <<= >>=
right-to-left
Programs
 Given two ints a and b. Write a code to display sum, sub,
mul, div
Java Programs