A blog for computer science passionates.

Wednesday, 4 March 2020

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...☺😊

Sunday, 1 March 2020

Hello Friends!!!

In this article, we are going to learn different operators in Java to work on different operands.
What are the Operators?
Operators are the symbols to perform different operations on operands (values or variables).
There are three different types of operators available in Java.
  • Unary
  • Binary
  • Ternary
Unary operators work on one operand (variable) only. Increment/ decrement operators are unary operators.
Increment Operators: These types of operators increase the value of a variable with one value only.
Decrement Operators: These types of operators decrease the value of a variable with one value only.
It has two different types:
  • Prefix Increment/Decrement
    • For example, ++a Increases the value of a with one value
    • --a Decreases the value of a with one value.
  • Postfix Increment/Decrement
    • For example, a++ Increases the value of a with one value
    • a-- Decreases the value of a with one value.
Difference between prefix/postfix: When we work with a prefix for example if we write ++a it means to increase the value of a first then work for the next step, and when we work with postfix for example if we write a++ it means to perform the operation with current value then increase the value of a.

Binary operators work on two operands (variable/value). There are lots of different binary operators available in Java. Those are,

=
Assignment Operator
a=b (a=5)
Arithmetic Operators
+
Addition
a+b
-
Subtraction
a-b
*
Multiplication
a*b
/
Division
a/b
%
Modulus
a%b
+=
Addition assignment
a+=b
-=
Subtraction assignment
a-=b
*=
Multiplication assignment
a*=b
/=
Division assignment
a/=b


Example of Arithmetic Operator,
The output of the above program,
Fig. The output of the Arithmetic Operators

In the next article, we will learn more operators. Till then try with arithmetic operators.
Happy Coding...☺😊