10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

15.22 Bitwise and Logical Operators EXPRESSIONS<br />

508<br />

15.22 Bitwise and Logical Operators<br />

<strong>The</strong> bitwise operators and logical operators include the AND operator &, exclusive<br />

OR operator ^, and inclusive OR operator |. <strong>The</strong>se operators have different<br />

precedence, with & having the highest precedence and | the lowest precedence.<br />

Each of these operators is syntactically left-associative (each groups left-to-right).<br />

Each operator is commutative if the operand expressions have no side effects.<br />

Each operator is associative.<br />

AndExpression:<br />

EqualityExpression<br />

AndExpression & EqualityExpression<br />

ExclusiveOrExpression:<br />

AndExpression<br />

ExclusiveOrExpression ^ AndExpression<br />

InclusiveOrExpression:<br />

ExclusiveOrExpression<br />

InclusiveOrExpression | ExclusiveOrExpression<br />

<strong>The</strong> bitwise and logical operators may be used to compare two operands of<br />

numeric type or two operands of type boolean. All other cases result in a compile-time<br />

error.<br />

15.22.1 Integer Bitwise Operators &, ^, and |<br />

When both operands of an operator &, ^, or| are of a type that is convertible<br />

(§5.1.8) to a primitive integral type, binary numeric promotion is first performed<br />

on the operands (§5.6.2). <strong>The</strong> type of the bitwise operator expression is the promoted<br />

type of the operands.<br />

For &, the result value is the bitwise AND of the operand values.<br />

For ^, the result value is the bitwise exclusive OR of the operand values.<br />

For |, the result value is the bitwise inclusive OR of the operand values.<br />

For example, the result of the expression 0xff00 & 0xf0f0 is 0xf000. <strong>The</strong><br />

result of 0xff00 ^ 0xf0f0 is 0x0ff0.<strong>The</strong> result of 0xff00 | 0xf0f0 is 0xfff0.<br />

DRAFT<br />

15.22.2 Boolean Logical Operators &, ^, and |<br />

When both operands of a &, ^,or| operator are of type boolean or Boolean, then<br />

the type of the bitwise operator expression is boolean. In all cases, the operands<br />

are subject to unboxing conversion (§5.1.8) as necessary.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!