Monday, 3 February 2014

Bit Wise Operators

The Java programming language provides operators that perform bit wise and bit shift operations on integral types.
Operator Description
~ Inverts a bit pattern
<< Perform Left shift
>> Perform Right Shift
>>> Perform unsigned right shift
& Bit wise AND
| Bit wise OR
^ Bit wise Exclusive OR

Before discuss about the Bit wise operators, will discuss about Binary representations of a number

1's Complement
1's complement simply inverts the bits, if the bit is set to zero, it sets the bit to 1 and if the bit is set to 1 it sets the bit to zero.

Ex:
8 in binary            = 0 0 0 0 1 0 0 0
1's complement(8) = 1 1 1 1 0 1 1 1

2's complement
Negative numbers are stored in 2's complement form. i.e., -8 is 2's complement of 8.
2's complement is sum of (1's complement of a number + 1)

Ex:
8 in binary            = 0 0 0 0 1 0 0 0
1's complement(8) = 1 1 1 1 0 1 1 1
2's complement(8) =               (+) 1
                                             -----------------
                                             1 1 1 1 1 0 0 0
                                              ----------------

there for -8 in 2's complement form is 11111000

Inversion (~)
Inversion operator change the bit value to zero, if it is one and change the bit value to one if it is zero.

Example
byte a = 7;

a in binary format is       0 0 0 0 0 1 1 1
~(a) in binary format is  1 1 1 1 1 0 0 0
~(a) represents -8 in binary format.

Example
class BitWiseEx{
 public static void main(String args[]){
  byte byteVar = 7;
  
  System.out.println("Value of Byte variable is " + byteVar);
  System.out.println("Value of Byte variable after inversion is " + ~(byteVar));
 }
}

Output
Value of Byte variable is 7
Value of Byte variable after inversion is -8

Bit Wise Left Shift(<<)
Left shift operator simply shifts all the bits to one bit left, i.e, it simply doubles the value.

Syntax:
    variable << n

    left shift the variable by 'n' number of times.

    Variable << n is equals to variable = variable * 2 power n

        int var = 2;
        var << 1 = 2 * 2 power 1 = 4
        var << 2 = 2 * 2 power 2 = 8
        var << 9 = 2 * 2 power 9 = 1024

Example
class BitWiseEx{
 public static void main(String args[]){
  byte byteVar = 7;
  
  System.out.println("Value of Byte variable is " + byteVar);
  System.out.println("After shifting 1 bit left " + (byteVar<<1));
  System.out.println("After shifting 2 bits left " + (byteVar<<2));
  System.out.println("After shifting 3 bits left " + (byteVar<<3));
  System.out.println("After shifting 4 bits left " + (byteVar<<4));
  System.out.println("After shifting 5 bits left " + (byteVar<<5));
  System.out.println("After shifting 6 bits left " + (byteVar<<6));
  System.out.println("Value of Byte variable is " + byteVar);
 }
}

Output
Value of Byte variable is 7
After shifting 1 bit left 14 
After shifting 2 bits left 28
After shifting 3 bits left 56 
After shifting 4 bits left 112 
After shifting 5 bits left 224 
After shifting 6 bits left 448 
Value of Byte variable is 7

Bit Wise Right Shift(>>)
   Right shift operator simply shifts all the bits to one bit right, i.e, it simply half (divide by 2) the value

   Syntax:
   variable >> n
      right shift the variable by 'n' number of times.

   Variable >> n is equals to variable = variable / 2 power n

   int var = 128;
   var >> 1 = 128 / (2 power 1) = 64
   var >> 2 = 128 / (2 power 2) = 32

Example
class BitWiseEx{
 public static void main(String args[]){
  int intVar = 128;
  
  System.out.println("Value of int variable is " + intVar);
  System.out.println("After shifting 1 bit Right " + (intVar>>1));
  System.out.println("After shifting 2 bits Right " + (intVar>>2));
  System.out.println("After shifting 3 bits Right " + (intVar>>3));
  System.out.println("After shifting 4 bits Right " + (intVar>>4));
  System.out.println("After shifting 5 bits Right " + (intVar>>5));
  System.out.println("After shifting 6 bits Right " + (intVar>>6));
  System.out.println("After shifting 6 bits Right " + (intVar>>7));
  System.out.println("Value of int variable is " + intVar);
 }
}

Output
Value of int variable is 128
After shifting 1 bit Right 64
After shifting 2 bits Right 32
After shifting 3 bits Right 16
After shifting 4 bits Right 8
After shifting 5 bits Right 4
After shifting 6 bits Right 2
After shifting 6 bits Right 1
Value of int variable is 128

Bit Wise unsigned Right Shift(>>>)
   shifts all the bits to one bit right, and make the left most bit to zero
class BitWiseEx{
 public static void main(String args[]){
  int intVar = -128;
  
  System.out.println("Value of int variable is " + intVar);
  System.out.println("After shifting 1 bit Right " + (intVar>>>1));
 }
}

Output
Value of int variable is -128
After shifting 1 bit Right 2147483584

Explanation
-128 in 2's complement form is

11111111 11111111 11111111 10000000

>>> shifts the one bit right and replace leftmost bit by zero

so (-128>>>1) is like below sequence

01111111 11111111 11111111 11000000

Bit Wise AND(&), OR(|), Exclusive OR(^)

Bit 1 Bit 2 & | ^
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0



Example
class BitWiseEx{
 public static void main(String args[]){
  byte a = 8;
  byte b = 9;
  
  System.out.println("a & b is " + (a&b));
  System.out.println("a | b is " + (a|b));
  System.out.println("a ^ b is " + (a^b));
 }
}


Output
a & b is 8
a | b is 9 
a ^ b is 1 


Explanation
   a    = 0000 1000
   b    = 0000 1001
   a&b = 0000 1000

   a    = 0000 1000
   b    = 0000 1001
   a|b = 0000 10001

   a     = 0000 1000
   b     = 0000 1001
   a^b = 0000 0001

Note:
   Bit wise operators are applicable only to integer values like byte, short etc., these are not applicable to float, double, Boolean.

Logical Operators                                                 other Operators                                                 Home

Sunday, 2 February 2014

Logical Operators

Java provides three logical operators, These are &&(AND), || (OR), and !(NOT)

Logical AND is also called Conditional AND

Logical OR is also called Conditional OR

Logical AND, OR are called Short circuit operators, will see why these are called short circuit soon.

Logical AND Operator
Operand1 Operand2 Evaluates To
TRUE TRUE TRUE
TRUE FALSE FALSE
FALSE TRUE FALSE
FALSE FALSE FALSE

As shown in the above table, && operator returns true if both the operands evaluates to true, otherwise returns false.

Example
class AndEx{
 public static void main(String args[]){
  boolean operand1 = true;
  boolean operand2 = true;
  System.out.println((operand1 && operand2)); 

  operand1 = true;
  operand2 = false;
  System.out.println((operand1 && operand2)); 

  operand1 = false;
  operand2 = true;
  System.out.println((operand1 && operand2)); 

  operand1 = false;
  operand2 = false;
  System.out.println((operand1 && operand2));
 }
}

Output
true
false
false
false

Why Logical AND is called short circuit operator
Since if the first statement in the expression evaluates to false, then java won't evaluates the entire expression. So Logical AND is called short circuit AND

Example
class ShortCircuitAnd{
 public static void main(String args[]){
  int a = 10, b=21;
  
  if( (a>b) && (a++ > b) ){
   System.out.println("This statement not evaluated");
  }
  System.out.println("a is not incremented " + a);
  
  if( (a<b) && (a++ < b) ){
   System.out.println("This statement is evaluated");
  } 
  System.out.println("a is not incremented " + a);
  
 }
}
   
Output
a is not incremented 10
This statement is evaluated
a is not incremented 11
Observation
In the expression (a>b) && (a++ > b), a>b is false, so && operator won't evaluates second statement in the expression, so a is not incremented.

Logical OR Operator
Operand1 Operand2 Evaluates To
TRUE TRUE TRUE
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE

As shown in the above table, || operator returns true if any of the operand evaluates to true, otherwise returns false.

Example
class OrEx{
 public static void main(String args[]){
  boolean operand1 = true;
  boolean operand2 = true;
  System.out.println((operand1 || operand2)); 

  operand1 = true;
  operand2 = false;
  System.out.println((operand1 || operand2)); 

  operand1 = false;
  operand2 = true;
  System.out.println((operand1 || operand2)); 

  operand1 = false;
  operand2 = false;
  System.out.println((operand1 || operand2));
 }
}
Output
true
true
true
false

Why Logical OR is called short circuit operator
Since if the first statement evaluates to true, then java won't evaluates the entire expression. So Logial OR is called short circuit OR

Example
class ShortCircuitOr{
 public static void main(String args[]){
  int a = 10, b=21;
  
  if( (a<b) || (a++ < b) ){
   System.out.println("This statement evaluated");
  }
  System.out.println("a is not incremented " + a);
  
  if( (a>b) || (a++ > b) ){
   System.out.println("This statement is evaluated");
  } 
  System.out.println("a is incremented " + a);
 }
}
  
Output
This statement evaluated
a is not incremented 10
a is incremented 11

Logical (!)NOT operator
Operand Evalutes To
FALSE TRUE
TRUE FALSE

If the operand is FALSE, ! Operator evaluates it to TRUE
If the operand is TRUE, ! Operator evaluates it to FALSE

Example
class LogicalNot{
 public static void main(String args[]){
  boolean a = true;
  System.out.println((!a));
  
  a = false;
  System.out.println((!a));
 }
}
Output
false
true





Not equal to operator                                                 Bitwise operators                                                 Home

Saturday, 1 February 2014

Conditional operators

Conditional operators determine if one operand is greater than, less than, equal to, or not equal to another operand.

Operator             Description
    ==                     equal to
    !=                      not equal to
    >                       greater than
    >=                     greater than or equal to
    <                       less than
    <=                     less than or equal to

Conditional operators takes two operands and return true, if the condition evaluates to true, otherwise return false.

Example
class ConditionalEx{
 public static void main(String args[]){
  int a = 10, b=11;
  
  System.out.println("is a equals to a " + (a==a));
  System.out.println("is a equals to b " + (a==b)); 
  System.out.println("is a not equals to a " + (a!=a));
  System.out.println("is a not equals to b " + (a!=b));
  System.out.println("is a less than a " + (a<a));
  System.out.println("is a less than b " + (a<b));
  System.out.println("is a less than or equal to a " + (a<=a));
  System.out.println("is a less than or equal b " + (a<=b));
  System.out.println("is a greater than a " + (a>a));
  System.out.println("is a greater than b " + (a>b));
  System.out.println("is a greater than or equal to a " + (a>=a));
  System.out.println("is a greater than or equal to b " + (a>=b));
 }
}

Output
is a equals to a true
is a equals to b false
is a not equals to a false
is a not equals to b true
is a less than a false
is a less than b true
is a less than or equal to a true
is a less than or equal b true
is a greater than a false
is a greater than b false
is a greater than or equal to a true
is a greater than or equal to b false

Some points to remember

1. You can compare one type of data with another if types are compatible.
class ConditionalEx{
 public static void main(String args[]){
  int a = 10;
  float b = 1.09f;
  char c = 'a';
  
  System.out.println("a less than b " + (a<b));
  System.out.println("a less than c " + (a<c));
 }
}
Output    

a less than b false
a less than c true
   
2. If you try to compare two incompatible type values, compiler will throw error.
class ConditionalEx{
 public static void main(String args[]){
  int a = 10;
  boolean b = true;
  
  System.out.println("a less than b " + (a<b));
 }
}
    

If you try to compile the above program, you will get the below error
ConditionalEx.java:8: error: bad operand types for binary operator '<'
 System.out.println("a less than b " + (a<b));
 ^
 first type: int
 second type: boolean
1 error
   


Arithmetic Operators                                                 Not equal to operator                                                 Home

Arithmetic Operators: ++, --

++ Increment operator; increments a value by 1
-- Decrement operator; decrements a value by 1

Pre increment
    Syntax:
    ++variable
      pre increment operator increments the variable value by 1 immediately.
  
Example
class PreIncrement{
 public static void main(String args[]){
  int var = 10;
  System.out.println(++var);
 }
}

Output
11

Post increment
    Syntax:
        variable++

    post increment operator increments the variable value by 1 after executing the current statement.


class PostIncrement{
 public static void main(String args[]){
  int var = 10;
  
  System.out.println(var++);
  System.out.println(var);
 }
}
Output

10
11

Pre Decrement
    Syntax:
        --variable

    pre decrement operator decrements the variable value by 1 immediately.
    Example
     
class PreDecrement{
 public static void main(String args[]){
  int var = 10;
  System.out.println(--var);
 }
}
Output

9

Post Decrement
    Syntax:
        variable--

post decrement operator decrements the variable value by 1 after executing the current statement.

class PostIncrement{
 public static void main(String args[]){
  int var = 10;
  System.out.println(var--);
  System.out.println(var);
 }
}

Output
10
9


Some more Examples
Class ArithmeticEx{
 public static void main(String args[]){
  int var = 10;
  
  int result = var++ + var++ ;
  System.out.println("value of variable is " + var);
  System.out.println("Result is " +result);
  
  result = var++ + ++var;
  System.out.println("value of variable is " + var);
  System.out.println("Result is " +result);

  result = var++ + var--;
  System.out.println("value of variable is " + var);
  System.out.println("Result is " +result);

  result = --var + var--;
  System.out.println("value of variable is " + var);
  System.out.println("Result is " +result);
 }
}
Output
value of variable is 12
Result is 21
value of variable is 14
Result is 26
value of variable is 14
Result is 29
value of variable is 12
Result is 26

Arithmetic Operators                                                 Conditional Operators                                                 Home