Bitwise Operators in Java

OperatorsBitwiseHexadecimal

In the previous blog we learned about the Operators in Java. If you want to know more about the operators visit Operators in Java. In this blog, we will go through the remaining operator from the last blog i.e. Bitwise Operator. Java provides bitwise operators used to perform bitwise operations on integer values. These operators are rarely used. The intent to explain these operators is to make you aware that these operators exist. These operators directly work on bits, meaning that for getting individual bits in numbers we can use masking techniques. Following are the bitwise operators:

Bitwise OperatorsDescription
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise Complement
<<Bitwise Left Shift
>>Bitwise Right Shift
>>>Unsigned Right Shift

Bitwise AND(&)

e.g.

public class BitwiseANDOperatorEx {
  public static void main(String args[]) {
    int x = 0xC6;
    int y = 0x59;
    System.out.println(x & y);
  }
}

Output:

64

It can be calculated as

0xC6 :
-----------------------------------------------------
C =>   8    4    2    1    6 =>   8    4    2    1
       1    1    0    0           0    1    1    0
-----------------------------------------------------
0xC6 =>  1100 0110

0X59:
-----------------------------------------------------
5 =>   8    4    2    1    9 =>   8    4    2    1
       0    1    0    1           1    0    0    1
-----------------------------------------------------
0x59 =>  0101 1001

Now we will multiply this two binary values i.e.

0XC6 =>     1  1  0  0    0  1  1  0
    X
0X59 =>     0  1  0  1    1  0  0  1
-----------------------------------------------------
            0  1  0  0    0  0  0  0


Binary to decimal :
0      1      0      0        0     0      0       0
-----------------------------------------------------
128=   64=    32=    16=      8=    4=     2=     1=
(27)   (26)   (25)   (24)     (23)   (22)   (21)   (20)
-----------------------------------------------------
(0*128)+(1*64)+(0*32)+(0*16)+(0*8)+(0*4)+(0*2)+(0*1) = 64

So it gives the value 64 after multiplying the two hexadecimal numbers.


Bitwise OR(|)

e.g.

public class BitwiseANDOperatorEx {
  public static void main(String args[]) {
    int x = 0xC6;
    int y = 0x59;
    System.out.println(x | y);
  }
}

Output:

223

It can be calculated as:

0xC6 :
-----------------------------------------------------
C =>   8    4    2    1    6 =>   8    4    2    1
       1    1    0    0           0    1    1    0
-----------------------------------------------------
0xC6 =>  1100 0110

0X59:
-----------------------------------------------------
5 =>   8    4    2    1    9 =>   8    4    2    1
       0    1    0    1           1    0    0    1
-----------------------------------------------------
0x59 =>  0101 1001

Now we will add this two binary values i.e.

0XC6 =>     1  1  0  0    0  1  1  0
    +
0X59 =>     0  1  0  1    1  0  0  1
-----------------------------------------------------
            1  1  0  1    1  1  1  1


Binary to decimal :
1      1      0      1        1     1      1      1
-----------------------------------------------------
128=   64=    32=    16=      8=    4=     2=     1=
(27)   (26)   (25)   (24)     (23)   (22)   (21)   (20)
-----------------------------------------------------
(1*128)+(1*64)+(0*32)+(1*16)+(1*8)+(1*4)+(1*2)+(1*1) = 223

So we get the value 223 after doing OR operation on two numbers.


Bitwise XOR(^)

e.g.

public class BitwiseANDOperatorEx {
  public static void main(String args[]) {
    int x = 0xC6;
    int y = 0x59;
    System.out.println(x ^ y);
  }
}

Output:

159

It can be calculated as:

0xC6 :
-----------------------------------------------------
C =>   8    4    2    1    6 =>   8    4    2    1
       1    1    0    0           0    1    1    0
-----------------------------------------------------
0xC6 =>  1100 0110

0X59:
-----------------------------------------------------
5 =>   8    4    2    1    9 =>   8    4    2    1
       0    1    0    1           1    0    0    1
-----------------------------------------------------
0x59 =>  0101 1001

Now we will XOR this two binary values i.e.

0XC6 =>     1  1  0  0    0  1  1  00X59 =>     0  1  0  1    1  0  0  1
-----------------------------------------------------
            1  0  0  1    1  1  1  1


Binary to decimal :
1      0      0      1        1     1      1      1
-----------------------------------------------------
128=   64=    32=    16=      8=    4=     2=     1=
(27)   (26)   (25)   (24)     (23)   (22)   (21)   (20)
-----------------------------------------------------
(1*128)+(0*64)+(0*32)+(1*16)+(1*8)+(1*4)+(1*2)+(1*1) = 159

We get 159 after calculating the two numbers.


Bitwise NOT(~)

Bitwise ~ is used to perform NOT operation. Bitwise ~ revere the values of the bits.

e.g.

0X59 =>     0  1  0  1    1  0  0  1
-----------------------------------------------------
~           1  0  1  0    0  1  1  0

Bitwise Left Shift(<<)

e.g.

public class BitwiseLeftShift {
  public static void main(String args[]) {

    int x = 2;

    System.out.println(x << 2);
  }
}

Output:

8

It can be calculated as:

jx << 2 :
-----------------------------------------------------
2 =>   8    4    2    1
       0    0    1    0
-----------------------------------------------------
2 =>  0010

Now we will shift bit left i.e.

x << 2 :    two time left shift
                0  0  0  0    0  0  1  0
Left Shift      0  0  0  0    0  1  0  0
Left Shift      0  0  0  0    1  0  0  0
-----------------------------------------------------
                0  0  0  0    1  0  0  0

Binary to decimal :
0      0      0      0        1     0      0       0
-----------------------------------------------------
128=   64=    32=    16=      8=    4=     2=     1=
(27)   (26)   (25)   (24)     (23)   (22)   (21)   (20)
-----------------------------------------------------
(0*128)+(1*64)+(0*32)+(0*16)+(1*8)+(0*4)+(0*2)+(0*1) = 8

Bitwise Right Shift(>>)

e.g.

public class BitwiseRightShift {
  public static void main(String args[]) {
    int x = 8;
    System.out.println(x >> 2);
  }
}

Output:

2

It can be calculated as:

x >> 2 :
-----------------------------------------------------
8 =>   8    4    2    1
        1    0    0    0
-----------------------------------------------------
8 =>  1000

Now we will shift bit right i.e.

x >> 2 :    two time Right shift
                0  0  0  0    1  0  0  0
Right Shift     0  0  0  0    0  1  0  0
Right Shift     0  0  0  0    0  0  1  0
-----------------------------------------------------
                0  0  0  0    0  0  1  0

Binary to decimal :
0      0      0      0        1     0      0       0
-----------------------------------------------------
128=   64=    32=    16=      8=    4=     2=     1=
(27)   (26)   (25)   (24)     (23)   (22)   (21)   (20)
-----------------------------------------------------
(0*128)+(0*64)+(0*32)+(0*16)+(0*8)+(0*4)+(1*2)+(0*1) = 2

Unsigned Right Shift(>>>)

e.g.

public class BitwiseRightShift {
  public static void main(String args[]) {

    int i = 16;
    System.out.println(i >>> 2);

    int j = -16;
    System.out.println(j >>> 2);
  }
}

Output:

4 1073741820

It can be calculated as:

i >>> 2 :
-----------------------------------------------------
16 =>   128  64   32   16   8    4    2    1
        0    0    0    1    0    0    0    0
-----------------------------------------------------
16 =>  0001 0000

Now we will shift bit Unsigned Right i.e.

x >>> 2 :    two time right shift
                0  0  0  1    0  0  0  0
Right Shift     0  0  0  0    1  0  0  0
Right Shift     0  0  0  0    0  1  0  0
-----------------------------------------------------
                0  0  0  0    0  1  0  0

Binary to decimal :
0      0      0      0        1     0      0       0
-----------------------------------------------------
128=   64=    32=    16=      8=    4=     2=     1=
(27)   (26)   (25)   (24)     (23)   (22)   (21)   (20)
-----------------------------------------------------
(0*128)+(0*64)+(0*32)+(0*16)+(0*8)+(1*4)+(0*2)+(0*1) = 4

j >>> 2
-----------------------------------------------------
16 =>
        128  64   32   16   8    4    2    1
        0    0    0    1    0    0    0    0
-16=> 2s Complement

        0000 0000 0000 0000 0000 0000 0001 0000
~       1111 1111 1111 1111 1111 1111 1110 1111
+                                              1
-----------------------------------------------------
-16 =>     1111 1111 1111 1111 1111 1111 1111 0000

Now we will shift bit Unsigned Right i.e.

x >>> 2 :    two time right shift
                1111 1111 1111 1111 1111 1111 1111 0000
Right Shift     1111 1111 1111 1111 1111 1111 1111 0000
Right Shift     0011 1111 1111 1111 1111 1111 1111 1100
-----------------------------------------------------
                0011 1111 1111 1111 1111 1111 1111 1100

Binary to decimal :
1111 1111 1111 1111 1111 1111 1111 1011
-----------------------------------------------------
...128=   64=    32=    16=      8=    4=     2=     1=
...(27)   (26)   (25)   (24)     (23)   (22)   (21)   (20)
-----------------------------------------------------
...(1*128)+(1*64)+(1*32)+(1*16)+(1*8)+(0*4)+(0*2)+(0*1) = 1073741820

Core Java Tutorial

Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.