What are the bitwise operators in Java?

Java 8Object Oriented ProgrammingProgramming

Java provides 7 bitwise operators which act on individual bits.

Operator
Description
Example
& Binary AND
It copies a bit to the result if it exists in both operands.
(A & B) = 12 means 0000 1100
| Binary OR
It copies a bit if it exists in either operand.
(A | B) = 61 means 0011 1101
^ Binary XOR
It copies the bit if it is set in one operand but not both.
(A ^ B) = 49 means 0011 0001
~ Binary One’s Complement
It is unary and has the effect of 'flipping' bits.
(~A ) = -61 means 1100 0011 in 2's complement form due to a signed binary number.
<< Binary Left Shift
The left operand value is moved left by the number of bits specified by the right operand.
A << 2 = 240 means 1111 0000
>> Binary Right Shift
The left operand value is moved right by the number of bits specified by the right operand.

A >> 2 = 15 means 1111
>>> Shift right zero fill
The left operand value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros.
A >>>2 = 15 means 0000 1111
raja
Published on 08-Feb-2018 12:49:23
Advertisements