Bitwise operator in java

Bitwise operator in java

There are many types of operators in java :

  1. Arithmetic operator 
  2. Unary operator
  3. Relational operator
  4. Logical operator
  5. Assignment operator
  6. Ternary operator
  7. Bitwise operator

Bitwise Operator :

 Bitwise Operators are used to manipulate the individual bit. bit means 0 and 1 .operation perform on bits. In Bitwise operator 1 is for true , 0 is for false.

Bitwise operator example 

  • &: Binary AND
  • |  :Binary OR
  • ^ :Binary XOR
  • ~ :Binary one's complement or Binary NOT
  • << :Binary left shift 
  • >> : Binary right shift 
Understand all these operators with examples:

Binary AND : In Binary AND if both bits are true then overall is  true .  

for example  :
calculate x= c & d , what will be the value of x? when c=0101 , d=0110 .
ANSWER : 0 1 0 0 

Binary OR : In Binary OR if any of one bit is true then true.

For example : 
calculate x= c | d , what will be the value of x? when c=0101 , d=0110 .
ANSWER : 0 1 1 1

Binary XOR : In Binary XOR  if the both bits  are  same then false , if both bits are different  then true .

calculate x= c ^ d , what will be the value of x? when c=0101 , d=0110 . 
ANSWER: 0 0 1 1

Binary one's complement or Binary NOT :  In this it give true value for false or false value for true. 

1's complement of 1 = 0 
1's complement of  0 =1
Example: Find the complement of a  if a= 01011
1's Complement of a is 10100  

Binary left shift  << : It shift the bit left side by the value . 

Example : Find A<<1 , if the value of A is 0101.
A<<1  , shift the bits of A left side  by one position .
ANSWER : 1 0 1 0

Binary right shift >>:  It shift the bit left side by the value . 

Example : Find A>>1 , If the value of A is 0101.
A>>1  , shift the bits of A right  side  by one position . 


ANSWER : 0 0 1 0 

Post a Comment

have you any doubt then ask.

Previous Post Next Post