Skip to main content

Short note on Bitwise operator in java.

            Operators are symbol that tells the computer to perform certain mathematical or logical function. We have 8 types of operator, bitwise is on of them. and also very important topic.

know more about operator.
---------------------------------------------------------------------------------------------

CONTENT.
  1. What is bitwise operator?
  2. Types of bitwise operator.
  3. Explanation of the types.
---------------------------------------------------------------------------------------------


What is bitwise operator?

        Java support  a powerful set of operator known as bitwise operator.

        The bitwise operator is used for controlling and interacting with hardware and also for manipulating data at the bit level.

Types of bitwise operator.

    There are six types of bitwise operator.
  • Bitwise AND ( & )
  • Bitwise OR ( | )
  • Bitwise Ex-OR ( ^ )
  • One's complement ( ~ )
  • Left shift ( << )
  • Right shift ( >> )

Explanation of the types of Bitwise operator.

1. Bitwise AND operator.

        AND operator gives the result 1, if both the operand bit is 1. The result is 0, if one of the operand is 0 or both the operator are 0.

        The Bitwise AND operator is representation by the symbol &. 

x

y

X & y

0

0

0

0

1

0

1

0

0

1

1

1

  
Example:

            int x=4, y=5,z;
            z=x&y;
     output: z=4

     Manual;

Binary value of 4

0000 0100

Binary value of 5

0000 0101

Result

0000 0100

 



2. Bit-wise OR operator.

            OR operator gives the result 0, if the both the operand bit is 0. The result is 1, if  one of the operand is 1 or both the operator are 1.

        The Bitwise OR operator is representation by the symbol |.

x

y

x| y

0

0

0

0

1

1

1

0

1

1

1

1

  
Example:

            int x=4, y=5,z;
            z=x|y;
     output: z=5

     Manual;

Binary value of 4

0000 0100

Binary value of 5

0000 0101

Result

0000 0101

 



Bit-wise complement operator.

            Complement operator gives the result 0, if operand bit is 1. The result is 1, if the operand is 0.

        The Bitwise Complement operator is representation by the symbol ~.

x

y

~x

0

0

1

0

1

1

1

0

0

1

1

0

  
Example:

            int x=4;
            z=~x;
     output: z=5

     Manual;

Binary value of 4

0000 0100

Result

1111 1011


Lift Shift operator.

        This operator is used to push the bit patterns to left to right by the number of bits given by the operator.

        The left Shift operator is represented by <<

General form:
              Variable = expression<<n;  ( where n is the number of bit position to be shifted.)

Example:
            int x=4.y;
            y=x<<2;
    output: y=16.

    manual:

Binary value of 4

0000 0100

Result

0001 0000


Right Shift operator.

        This operator is used to push the bit patterns to right to left by the number of bits given by the operator.

        The right Shift operator is represented by >>

General form:
              Variable = expression>>n;  ( where n is the number  of bit position to be shifted.)

Example:
            int x=4.y;
            y=x>>2;
    output: y=1.

    manual:

Binary value of 4

0000 0100

Result

0000 0001




Learning helps us to know the concept, practicing it with example helps us to store the concept in mind. 


Comments