Showing posts with label Bit-wise operators. Show all posts
Showing posts with label Bit-wise operators. Show all posts

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.