Translate

Tuesday, 21 June 2016

Java Binary Operators Tutorial

                                      Binary Operators

Def: Those operatos which are applied on two(2) operands known as binary operatos.

Type of binary operators:
                        1.Arithmetic operators
                        2.Relational operators
                        3.logical operators
                        4.Shift and bitwise operatos
                        5.Assignment operators

I will discuss about each of the above types with examples.If still you have doubt then comment below..
                          Arithmetic Operators

We all are very familiar with these operators.
Arithmetic operators are further classified in 2 type:
1.Multiplicative   like : *,/,%
2.Additive         like : +,-

Just read the demo for better understanding  gud luck.
DEMO :

public class Binary_arithmetic{
 public static void main(String args[]){
  int n1,n2;
  n1=10;
  n2=5;

  int sum,diff;
  sum=n1+n2;
  diff=n1-n2;

  //Additive operators
  System.out.println("n1 + n2 is :"+  sum);
  System.out.println("n1 - n2 is :"+  diff);

  //multiplicative operators
  System.out.println("n1 * n2 is :"+  n1*n2);
  System.out.println("n1 / n2 is :"+  n1/n2);
  System.out.println("n1 % n2 is :"+  n1%n2);


 }
}


output of above code is:
n1 + n2 is :15
n1 - n2 is :5
n1 * n2 is :50
n1 / n2 is :2
n1 % n2 is :0
                                       Relational Operators

Relational operators are also generally classified in two category:
1.Relational operators     like: <, >, <=, >=, instance of
2.Equality operators       like: ==, !=

Just read the demo for better understanding  gud luck.
DEMO :

public class Binary_relational{
 public static void main(String args[]){
  
  //Equality operators
  int a,b;
  a=10;
  b=9;

  if(a==b){
   System.out.println("a is equal to b");
  }
  
  if(a!=b){
   System.out.println("a is not equal to b");
  }


  //Relational operators

   if(a<b){
    System.out.println("a is less than b");
   }

   if(a>b){
    System.out.println("a is greater than b");
   }

   if(a<=b){
    System.out.println("a is less than equal to b");
   }

   if(a>=b){
    System.out.println("a is greater than equal to b");
   }


   //instance of 
   //this is used for finding that a object is belong to a class or not

   Binary_relational obj=new Binary_relational();

   if(obj instanceof Binary_relational){
    System.out.println("obj is instance of class Binary_relational");
   }


 }
}

output of above code is:
a is not equal to b
a is greater than b
a is greater than equal to b
obj is instance of class Binary_relational
                    Logical Operators

There are two types of relational operators :
And &&
Or ||

Just read the demo for better understanding  gud luck.
DEMO :


public class Binary_logical{
 public static void main(String args[]){
  //And &&  Or ||

  int a=9;
  int b=8;
  int c=10;

  if(a>b && a>c){
   System.out.println("a is largest among three");
  }

  if(a>b || a >c){
   System.out.println("a is greater than either from a or b or both");
  }
 }
}

output of above code is:

a is greater than either from a or b or both
                                 Shift Operators and Bitwise operators

Shift operators are : <<, >>, >>>
bitwise operators are : bitwise And &,bitwise Or |, bitwise ex-or ^

Just read the demo for better understanding  gud luck.
DEMO :

public class Binary_shift_bitwise{
 public static void main(String args[]){

  //shift operators
  int x=10;

  int left_shift=x<<1;
  System.out.println("left shift of x by 1 bit is :" + left_shift);

  int right_shift = x>>1;
  System.out.println("right shift of x by 1 bit is :" + right_shift);

  //>>>(zero filled right shift)
  int zero_filled_right_shift= x >>>1;
  System.out.println("zero_filled_right_shift of x by 1 bit is :" + zero_filled_right_shift);
 
     //bitwise operators
     /*  & (bitwise and)
            Binary AND Operator copies a bit to the result if it exists in both operands.

            Example: (A & B) will give 12 which is 0000 1100
        */

        int a,b;
        a=60;
        b=13;
           int and_result=a&b;
           System.out.println(" a & b is:" +  and_result);

        /* | (bitwise or)
           Binary OR Operator copies a bit if it exists in either operand.

           Example: (A | B) will give 61 which is 0011 1101
        */
            int or_result=a|b;
            System.out.println(" a | b is:" +  or_result);

           /*^ (bitwise XOR)
              Binary XOR Operator copies the bit if it is set in one operand but not both.

              Example: (A ^ B) will give 49 which is 0011 0001

           */
            int xor_result=a^b;
            System.out.println(" a ^ b is:" +  xor_result);

 }
}

output is:

left shift of x by 1 bit is :20
right shift of x by 1 bit is :5
zero_filled_right_shift of x by 1 bit is :5
 a & b is:12
 a | b is:61
 a ^ b is:49
                         Assignment Operators

there are many variations of this like ; =,+=,-=,*=,/=,%=,>>=,<<=,>>>=

a+=b  is same as a=a+b
a-=b is same as a=a-b
and so on

I think there is no need for demo in this if u still have doubt then comment below i will show u demo...

1 comment:

Basic Programming Knowledge said...

If u still have any doubt plz comment......

Working With Java Collections