A blog for computer science passionates.

Wednesday 4 March 2020

Java: Operators Part-2 Bitwise Operator

Hello Friends!!!
Today we are going to learn Bitwise Operator, in the previous article we have learned Assignment and Arithmetic operator.
Arithmetic and assignment operators work with operand (variables & values) directly, bitwise operators work on bits of int, long, short, char, byte datatype operands. Let's understand how many bitwise operators are there and how do bitwise operators work?


Bitwise Operators
~
Not (Unary)
~a
&
And
a&b
|
Or
a|b
^
Exclusive Or
a^b
>> 
Shift Right
a>>b
>>> 
Shift Right with zero fill
a>>>b
<< 
Shift Left
a<<b
&=
And Assignment
a&=b
|=
Or Assignment
a|=b
^=
Ex-Or Assignment
a^=b
>>=
Shift Right Assignment
a>>=b
>>>=
Shift Right Assignment with zero fill
a>>>=b
<<=
Shift Left Assignment
a<<=b

Fig. Bitwise Operators

How do bitwise operators work?

Fig. Bitwise Operator with a bit values

Let's make it easy to use and work let's take two variables A=5 and B=2.
Now convert values of A & B to binary so, A=0000 0101 and B=0000 0010 and perform some operations based on the above figure.
Fig. Bitwise Operations
Check the above figure for and, or, ex-or, and for negation, Java performs two's complement. Try to calculate the above operations and check the 👇 example and get the answer(output).

That's it for this article, in the next article we will learn more operators of Java. Till then
Happy Coding...☺😊

No comments:

Post a Comment