wellnwill Marathahalli Bangalore

Bitwise OR Operator

Returns true if at least one operand evaluate to true.Operands can be boolean type as well as integral type also.

operand 1 operand 2 Result (|)
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE
TRUE TRUE TRUE
operand 1 operand 2 Result (|)
0 0 0
1 0 1
0 1 1
1 1 1
x=4 0 1 0 0
y=2 0 0 1 0
x|y 0 1 1 0

Evaluation of bitwise OR (|)

class Test78
{
   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, true, true
class Test79
{
   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 - 6, 1,0, 1

Logical AND

It is same as bitwise AND(&), but if the operand on the left returns false, it returns false without evaluating the operand on the right.

operand 1 operand 2 Result (&&)
TRUE FALSE FALSE
FALSE TRUE FALSE
FALSE FALSE FALSE
TRUE TRUE TRUE
class Test81
{
  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 Test82
{
  public static void main(String args[])
  {
    System.out.println(1&&1);
    System.out.println(0&&0);
    System.out.println(1&&0);
    System.out.println(0&&1);
  }
}

// O/P - CompileTimeException
       - bad operand type for binary operator ‘&&’.
class Test83
{
  public static void main(String args[])
  {
    System.out.println(10>20&&20>10);
  }
}

// O/P - false

class Test84
{
  public static void main(String args[])
  {
    int a=10;
    System.out.println((a++ < ++a)&&(20>10));
  }
}

// O/P - true

Logical OR

It is same as bitwise OR(|), but if the operand on the left returns true, it returns true without evaluating the operand on the right.

operand1 operand2 Result(||)
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE
TRUE TRUE TRUE
class Test85
{
  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, true, true

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

// O/P - CompileTimeException
       - bad operand type for binary operator ‘&&’.
class Test87
{
  public static void main(String args[])
  {
    System.out.println(10<20||20>10);
  }
}

// O/P - true

class Test88
{
  public static void main(String args[])
  {
    int a=10;
    System.out.println((a++ > ++a)||(20>10));
 }
}

// O/P - true

Exclusive OR Operator (XOR)

  • Returns true if one and only one — of the operand evaluates to true. Returns false if both operands evaluate to true or if both operands evaluate to false.
  • Operands can be boolean as well as integral type also.
operand1 operand2 Result(^)
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE
TRUE TRUE FALSE
operand1 operand2 Result(^)
1 1 0
0 0 0
1 0 1
0 1 1
class Test89
{
 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 - false, false, true, true
class Test90
{
  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 - 6, 1, 0, 0

Evaluation of XOR (^)

x=4 0 1 0 0
y=2 0 0 1 0
X^y 0 1 1 0
Footer with Map