Java Reference

In-Depth Information

r = p | q; /* Binary representation 61 = 0011 1101 */

System.out.println("p | q = " + r );

r = p ^ q; /* Binary representation 49 = 0011 0001 */

System.out.println("p ^ q = " + r );

r = ~p; /* Binary representation -61 = 1100 0011 */

System.out.println("~p = " + r );

r = p << 2; /* Binary representation 240 = 1111 0000 */

System.out.println("p << 2 = " + r );

r = p >> 2; /* Binary representation 15 = 1111 */

System.out.println("p >> 2 = " + r );

r = p >>> 2; /* Binary representation 15 = 0000 1111 */

System.out.println("p >>> 2 = " + r );

}

}

This would produce the following result:

p & q = 12

p | q = 61

p ^ q = 49

~p = -61

p << 2 = 240

p >> 15

p >>> 15

Operators Precedence and Associativity

.

Every operator has a precedence and associativity associated with it. Precedence of oper-

ators is used in determining the sequence of operators for evaluating an expression in case

expression has a number of operators. In an expression, the operators having higher pre-

cedence are evaluated first.

Associativity is the concept for determining the evaluation order of operators when there

are multiple operators with same precedence then associative determines either to be eval-

uated from left to right or right to left.