Java – Bitwise Operators

Home/Java - Tutorial/Java - Operators/Java – Bitwise Operators
Java – Bitwise Operators 2017-08-09T09:29:59+00:00

OPERATOR

MEANING

EXPLANATION

EXAMPLE

RESULT

~

Bitwise unary NOT

This sign is used for inverts all the bits

~42

213

&

Bitwise AND

Produce a 1 bit if both operands are also 1 otherwise 0

2 & 7

2

|

Bitwise OR

either of the bits in the operands is a 1,

then the resultant bit is a 1 otherwise 0

2 | 7

7

^

Bitwise exclusive OR

if exactly one operand is 1, then the result

is 1. Otherwise, the result is zero

2 ^ 7

5

>> 

Shift right

The right shift operator, >>, shifts all of the bits in a value to the right a specified number

of times.

7 >> 2

1

>>> 

Shift right zero fill

shift a zero into the high-order bit no matter

what its initial value was

-1 >>> 30

3

<< 

Shift left

The left shift operator, <<, shifts all of the bits in a value to the left a specified number

of times.

2 << 2

8

&=

Bitwise AND assignment

This is a short sign of AND operation on same variable

a=2

a& = 2

a = 2

|=

Bitwise OR assignment

This is a short sign of OR operation on same variable

a=2

a| = 7

a = 7

^=

Bitwise exclusive OR assignment

This is a short sign of XOR operation on same variable

a = 2

a^ = 7

a = 5

>>=

Shift right assignment

This is a short sign of shift right operation on same variable

a = 7

a>>  =  2

a = 1

>>>=

Shift right zero fill assignment

This is a short sign of shift right zero fill operation on same variable

a = -1

a>>> = 30

a = 3

<<=

Shift left assignment

This is a short sign of shift left operation on same variable

a = 2

a<< = 2

a = 8

EX.

 

Output :

…………..Bitewise operators……………..
<——-Bitewise Logical Operators——->
The binary value of a = 0010
The binary value of b = 0111
The Bitwise OR : a | b = 0111 and Decimal value = 7
The Bitwise AND : a & b = 0010 and Decimal value = 2
The Bitwise XOR(exclusive OR) : a ^ b = 0101 and Decimal value = 5
The Bitwise unary NOT : ~a & a = 0000 and Decimal value = 0
~a&b|a&~b = 0101 and Decimal value = 5
<——-Bitewise Shift Operators——->
The original binary value of a = 0010 and Decimal value of a = 2
The Left shift : a = a << 2 means a = 0010 << 2 hence a = 1000 and Decimal value of a = 8
The original binary value of b = 0111 and Decimal value of b = 7
The Right shift : b = b >> 2 means b =  0111 >> 2 hence b = 0001 and Decimal value of b = 1
The original decimal value of u = -1
The Unsigned Right shift : u = u >>> 30 means u = 11111111 11111111 11111111 11111111 >>> 30 hence u = 0011 and Decimal value of u = 3
<——-Bitewise Assignment Operators——->
The original binary value of p = 0101 and Decimal value of p = 5
The Bitewise Shift Right Assignment Operators : p >>= 2 means p = p >> 2 hence p = 0101 >> 2 so p = 0001 and Decimal value of p = 1

Prev Next