wellnwill Marathahalli Bangalore

Logical Operator

Operator Name
! Not
& Bitwise AND
| Bitwise OR
&& Logical AND
|| Logical OR
^ XOR

Not Operator

Returns true if expression evaluates false. Returns false if expression evaluates true.


operand Result(!)
TRUE FALSE
FALSE TRUE

class Test72
{
  public static void main(String args[])
  {
    System.out.println(!true);
    System.out.println(!false);
  }
}

// O/P -  false,true
class Test74
{
   public static void main(String args[])
   {
     System.out.println(!(10<20));
   }
}


// O/P -  false

The operands of this operator will be boolean type only. We can not apply this operator to integral value.

class Test73
{
   public static void main(String args[])
   {
     System.out.println(!1);
     System.out.println(!0);
   }
}

// O/P - CompileTimeError
       - bad operand type int for unary operator'!'.

Bitwise AND Operator

Returns true if both operands evaluate to true. Operands can be Boolean type as well as integral type also.
operand 1 operand 2 Result (&)
TRUE FALSE FALSE
FALSE TRUE FALSE
FALSE FALSE FALSE
TRUE TRUE TRUE
operand 1 operand 2 Result (&)
0 0 0
1 0 0
0 1 0
1 1 1
class Test75
{
   public static void main(String args[])
   {
     System.out.println(true&true);
     System.out.println(false&false);
     System.out.println(true&false);
     System.out.println(false&true);
   }
}

// O/P - true, false, false, false 
class Test76
{
   public static void main(String args[])
   {
     System.out.println(4&2);
     System.out.println(1&0);
     System.out.println(0&0);
     System.out.println(1&1);
 }
}

// O/P - 0, 0, 0, 1

Evaluation of bitwise AND(&)

x=4 0 1 0 0
y=2 0 0 1 0
x&y 0 0 0 0
class Test77
{
  public static void main(String args[])
  {
    int x=1;
    System.out.println(10<20&20>10);
    System.out.println(x++==++x);
  }
}

// O/P -  true,false
Footer with Map

Top