Thursday, October 31, 2013

Operators [Bitwise Operators]



Bitwise operators

Operator
Name
Example

Description
Expr1 & expr2
Bitwise and
7 & 5
7 = 111
5 = 101
      101
Answer = 5, 1 if both bits are 1 else 0.
Expr1 & expr2
Bitwise and
7 | 5
7 = 111
5 = 101
       111
Answer= 7, 0 if both bits are 0 or else 1.
Expr1 | expr2
Bitwise xor
5 ^ 7
7 = 111
5 = 101
       010
Answer= 2, 1 if odd nos. of 1 present.
~expr1
Bitwise not
~3

(~expr 1) will give -4 which is 1100 in 2's complement form due to a signed binary number.
Expr1 << expr2
Bitwise left shift
10 << 3
10 * 23
Answer=80,  Num * 23
Expr1 >> expr2
Bitwise right shift
20>>3
20 / 23
Answer=2,  Num / 23
Expr1 >>> expr2
Bitwise Unsigned Right Shift
-10 >>>2
10 = 1010
Answer=2

Bitwise Operator
Example


    public class Bitwise_Operator
        {
            public static void main(String args[])
                {
                    Bitwise_Operator bp=new Bitwise_Operator();
                    bp.BitOP();
                    bp.BitShift();
                  
                }
            void BitOP()
                {
                    int Num1=25; // 11001
                    int Num2=5; //binary 00101
                    System.out.println("Value of Num1 and Num2 is = "+Num1+" , "+Num2);
                    System.out.println("Binary value of Num1 is 11001");
                    System.out.println("Binary value of Num2 is 00101 ");
                    System.out.println("\nResult after Bitwise AND (multiplication)" );
                    System.out.println("Num1 & Num2 = " + (Num1 & Num2));
                    System.out.println("\nResult after Bitwise OR (Addition)" );
                    System.out.println("Num1 | Num2 = " + (Num1 | Num2));
                    System.out.println("\nResult after Bitwise Ex-OR" );
                    System.out.println("Num1 ^ Num2 = " + (Num1 ^ Num2));
                    System.out.println("\nResult after Bitwise NOT" );
                    System.out.println("~Num1 = " + (~Num1));
                   
                }
            void BitShift()
                {
                    int Num1=75,Num2=-25;
                    System.out.println("Value of Num1 = "+Num1);
                    System.out.println("Binary value of Num1 = 1001011");
                    System.out.println("Signed Right Shift by 3 Num1/2^3= " + (Num1>>3) ); 
                    System.out.println("Signed Left Shift by 3 Num1*2^3= " + (Num1<<3 span="" style="mso-spacerun: yes;"> 
                    System.out.println("Value of Num2 = "+Num2);
                    System.out.println("Binary value of Num2 = 110010");
                    System.out.println("Unsigned Right Shift by 2 = " + (Num2>>>2) );
       
                }
        }


Output


Value of Num1 and Num2 is = 25 , 5
Binary value of Num1 is 11001
Binary value of Num2 is 00101

Result after Bitwise AND (multiplication)
Num1 & Num2 = 1

Result after Bitwise OR (Addition)
Num1 | Num2 = 29

Result after Bitwise Ex-OR
Num1 ^ Num2 = 28

Result after Bitwise NOT
~Num1 = -26
Value of Num1 = 75
Binary value of Num1 = 1001011
Signed Right Shift by 3 Num1/2^3= 9
Signed Left Shift by 3 Num1*2^3= 600
Value of Num2 = -25
Binary value of Num2 = 110010
Unsigned Right Shift by 2 = 1073741817


Thursday, October 3, 2013

Operators [Logical Operators]




Logical Operators

To check multiple conditions at one time we need logical operator. These operators also known as short-circuit logical operators.

&&
Logical AND
expr1 && expr2
True if both expression are true else return false
||
Logical OR
expr1 && expr2
True if either expression is true,  return false if both expressions are false
!
Logical NOT
!expr1
Return true if expr1 is NOT true or else return false.

AND (&& Operator)

A multiple relational expression can be combine using && operator. Return true if all the condition are true or else return false if any one of the condition failed.

 OR (|| Operator)

A multiple relational expression can be combine using || operator. Return true if any one of the condition is true or else return false if none of the condition satisfied.

Not (! Operator)

Not Operator used with any relational expression or logical expression. It is also used with a boolean variable. It is a compliment operator.

Example

    public class Logical_Operator
        {
            public static void main(String args[])
                {
                    Logical_Operator op=new Logical_Operator();
                    op.ANDOP();
                    op.OROP();
                    op.NOTOP();
                }

            void ANDOP()
                {
                    int a=10,b=20,c=30;
                    System.out.println("\nLogical AND\n");
                    if(a>b && a>c)
                        {
                            System.out.println(a+" is bigger ");
                        }
                    if(b>a && b>c)
                        {
                            System.out.println(b+" is bigger ");
                        }
                    if(c>a && c>b)
                        {
                            System.out.println(c+" is bigger ");
                        }      
                }

            void OROP()
                {
                    int age=55;
                    System.out.println("\nLogical OR\n");
                    if(age<18 age="">60)
                        {
                            System.out.println(" Not elligible for the job ");
                        }
   
                }               
          
 void NOTOP()
                {
                    boolean t1 =true;
                    boolean t2=false;
                    System.out.println("\nLogical NOT\n");
                    if(t1)
                        {
                            System.out.println(" You hit the bulls eye ");
                        }
                    if(!t2)
                        {
                            System.out.println(" You failed to hit the bulls eye ");
                        }                       
   
                }                            
        }


Output



Logical AND

30 is bigger

Logical OR


Logical NOT

 You hit the bulls eye
 You failed to hit the bulls eye