Thursday, 28 May 2015

Java Bit-wise operators

The Java supports the following bit-wise operators:

~ (1’s complement or bit-wise complement)| (bit-wise OR)

& (bit-wise AND)

^ (bit-wise exclusive OR or XOR)

The operands of these operators must be of integer type only. If any of the operand of a bit-wise operator is of type byte, short or char then it is promoted to int before being used. If one operand is of type long then other is also promoted to long. So the type of the result will always be int or long.

Note: The operators |, & and ^ behave like boolean logical operators if both the operands are of boolean type.

Bit-wise Assignment Operators

The Java supports the following Bit-wsie Assignment Operators:

&=, |=, ^=

An assignment operator has the following syntax:

<variable><operator> = <expression>

The above assignment is equivalent to:

<variable> = <variable>  <operator> (<expression>)

For example the assignment:

 i &= i1 is equivalent to i = i & i1

Here i is an integer type variable and i1 is an integer expression.

Java Assignment Operator (=)

The assignment operator has the following syntax:

<variable> = <expression>

Here variable can be of primitive type or reference type. Similarly expression may result in a primitive data value or object reference:

Example: Assignment involving primitives.

x = 10;

Example: Assignment involving reference data type.

int x[];

x = new int [100];

Here new is an operator that returns an object reference.

Multiple Assignments

The assignment operator = may be used like any other operator to form a compound expression.

The operator can appear more than once in an assignment statement as shown below:

Example:

int x = 5, y = 6 , z = 7;

x = y = z; //multiple assignment statement

Here = behaves like an operator. It is a right associative operator, hence after the execution of the assignment statement, the value of variables x, y and z will be 7.

Example:

int  x[], y[];

x = y = new int[20];

Here new operator returns reference to an array, which can hold 20 int values. The reference is first assigned to y and then to x as the = operator is right associative.

Note: Assigning a reference does not create a copy of the object. So both x and y refer to the same array in this example.

Java Ternary Conditional Operator (? :)

The ternary conditional operator has the following syntax:

<condition> ? <expr1> : <expr2>

If condition (boolean expression) is true then <expr1> is evaluated otherwise <expr2> is evaluated.

For example, the following code segment, will store the maximum of x and y into variable max:

max = (x > y) ? x : y;

Here it is assumed that the variables x, y and max are of numeric type. The type of the variable max should be higher or same as the higher type among types of x and y.

Java Conditional Operators (or Short-Circuit Logical Operators)

The Java supports the following conditional operators:

|| (conditional OR)

&& (conditional AND)

The conditional operators && and || can be used to perform logical OR or AND operations on boolean operands. The operators are similar to boolean logical operators. The only difference is that if the result is definitely known after evaluating the first operand then second operand is not evaluated as discussed in the previous section. Hence these operators are also known as short-circuit logical operators. Sometimes this proves to be very useful in avoiding run-time exceptions.

For example, the following piece of code may result in a run-time exception, if the value of x is zero (assuming that x and y are integers), as it will lead to division by zero:

x = 0;

if( x != 0 & y/x > 5)

{

----

----

}

The possibility of the run-time exception can be avoided if we use the short-circuit operator && instead of boolean logical operator & as shown below:

x = 0;

if( x != 0 && y/x > 5)

{

----

----

}

Java Boolean Logical Operators

The Java supports the following boolean logical operators:

! (logical complement)

| (logical OR)

& (logical AND)

^ (logical exclusive OR)

Logical Complement ( ! ) Operator

This is a unary operator and the operand must always be a boolean expression. This acts as the negation operator, which negates a boolean expression as shown in the following table:

Boolean Expression(b) Logical Complement(!b)
true false
false true

Logical OR ( | ) Operator

The logical OR operator is used to combine two boolean expressions to form a compound boolean expression. The value of any boolean expression in Java is either true or false no matter how complex it is. The following table shows the value of boolean expression obtained by combining two boolean expressions using logical OR operator.

Boolean Expression(b1) Logical Complement(b2) Logical Complement(b1 | b2)
false false false
false true true
true false true
true true true

If first boolean expression is true then value of the compound expression will be true irrespective of the value of the second boolean expression. Even then the second boolean expression is always evaluated. This is different from the conditional (short-circuit) OR operator where the second boolean expression is not evaluated if the result of the first boolean expression is true.

If both the operands of the | operator are of integer type then this operator behaves like bit-wise OR operator.

Logical AND ( & ) Operator

The logical AND operator is used to combine two boolean expressions to form a compound boolean expression. The value of any boolean expression in Java is either true or false no matter how complex it is. The following table shows the value of boolean expression obtained by combining two boolean expressions using logical AND operator.

Boolean Expression(b1) Logical Complement(b2) Logical Complement(b1 & b2)
false false false
false true false
true false false
true true true

If first boolean expression is false then value of the compound expression will be false irrespective of the value of the second boolean expression. Even then the second boolean expression is always evaluated. This is different from the conditional (short-circuit) AND operator where the second boolean expression is not evaluated if the result of the first boolean expression is false.

If both the operands of the & operator are of integer type then this operator behaves like bit-wise AND operator.

Logical Exclusive OR or XOR ( ^ ) Operator

The logical exclusive OR operator is used to combine two boolean expressions to form a compound boolean expression. The value of any boolean expression in Java is either true or false no matter how complex it is. The following table shows the value of boolean expression obtained by combining two boolean expressions using logical exclusive operator.

Boolean Expression(b1) Logical Complement(b2) Logical Complement(b1 ^ b2)
false false false
false true true
true false true
true true false

If both the operands of the ^ operator are of integer type then this operator behaves like bit-wise exclusive OR operator.

Boolean Logical Assignment Operators

The Java supports the following Boolean Logical Assignment Operators:

&=, |=, ^=

An assignment operator has the following syntax:

<variable><operator> = <expression>

The above assignment is equivalent to:

<variable> = <variable>  <operator> (<expression>)

For example the assignment:

 b &= b1 is equivalent to b = b & b1

Here b is a boolean variable and b1 is a boolean expression