Bitwise Operators in Java

In this article, we will learn about all Bitwise Operators in Java.

What are Operators in Java?

In programming languages, the operator is a symbol that tells the interpreter/Compiler to perform specific logical, bitwise, relational, or mathematical operations and produce the desired output.

What is Bitwise operator in Java?

Bitwise Operators are used for manipulating data at the bit level, these operators are used to perform manipulation on individual bits of a number.

There are different types of bitwise operators in java, namely:

  •  Bitwise AND
  •  Bitwise OR
  •  Bitwise NOT
  • Bitwise XOR/exclusive OR
  • Bitwise Shift operators

Let’s look at each one of these in details.

Bitwise AND:

It’s a Binary operator 

The result of AND is 1 when both bits are 1 otherwise 0

Symbol: &

Example:

Bitwise AND of numbers 4,6

The binary representation of 4 → 100
The binary representation of 6 → 110
                                 === 
Result:                          100 → 4(In decimal)
                                 ===

Code:

Java Code

class Bitwise {
    public static void main(String[] args) {
        int a = 4;
        int b = 6;
        System.out.println(AND(a,b));
    }

    private static int AND(int a,int b) {

        int ans = a&b;
        return ans;
    }
}

Output: 4

Bitwise OR:

It’s a Binary operator 

The result of OR is 1 when any one of the bits is 1 otherwise 0

Symbol: |

Example:

Bitwise OR of numbers 4,6

The binary representation of 4 → 100
The binary representation of 6 → 110
                                 === 
Result:                          110 → 6(In decimal)
                                 ===

Code:

Java Code

 class Bitwise {
    public static void main(String[] args) {
        int a = 4;
        int b = 6;
        System.out.println(OR(a,b));
    }

    private static int OR(int a,int b) {
        //Bitwise OR of 4,6
        int ans = a|b;
        return ans;
    }
}

Output: 6

Bitwise XOR:

It’s a Binary operator 

The result of XOR is 1 when both bits are different and 0 if both bits are the same

Symbol: ^

Example:

Bitwise XOR of numbers 4,6

The binary representation of 4 → 100
The binary representation of 6 → 110
                                 === 
Result:                          010 → 2(In decimal)
                                 ===

Code:

Java Code

 class Bitwise {
    public static void main(String[] args) {
        int a = 4;
        int b = 6;
        System.out.println(XOR(a,b));
    }

    private static int XOR(int a,int b) {
        //Bitwise XOR of 4,6
        int ans = a^b;
        return ans;
    }
}

Output: 2

Bitwise NOT:

It’s a Unary operator 

The result of NOT is 1 when the bit is 0 and vice versa

It complements each bit one by one 

Symbol: ~

Example:

NOT of 7?

Binary representation of 7 → 0 1 1 1 
Complement:                  ======= 
Result:                      1 0 0 0 → 8(In decimal)
                             =======
Therefore,  ~7 = 8

We get the answer as 2’s complement of a result  obtained after performing NOT (8 in our case)

So 2’s complement of 8 is -8, Hence we get -8 as the answer (Note for every number it is not the case that we will get -ve of it like in this case we got -8 as 2’s complement for 8 but it is not for all)

Code:

Java Code

 class Bitwise {
    public static void main(String[] args) {
        int a = 7;
        System.out.println(NOT(a));
    }

    private static int NOT(int a) {
        //Bitwise NOT of 7
        int ans = ~a;
        return ans;
    }
}

Output: -8

Shift Operators:

  • Signed left shift
  • Signed right shift
  • Unsigned right shift
  • Unsigned left shift

Signed Left shift:

 It shifts bits by specified positions to left

Symbol: “<<“

  • num<<2  //Shifts 2 bits
  • num<<3  //Shifts 3 bits
  • num<<n → Binary bits of num will move n bits towards the left.

Example:

1<<2, here 1 will be converted to binary (0001) 
      and all the bits will move 2 bits.

Results in → 0100 (4 in decimal)

Code:

Java Code

 class Bitwise {
    public static void main(String[] args) {
        int num = 1;
        int n = 2;
        System.out.println(s_left(num,n));
    }

    private static int s_left(int a,int n) {
        //Shift left
        int ans = a<<n;
        return ans;
    }
}

Output: 4

Signed Right shift:

It shifts bits by specified positions to Right

Symbol: >>

  • num >> 2  //Shifts 2 bits to right 
  • num >> 3  //Shifts 3 bits to right
  • num >> n → Binary bits of num will move n bits towards the right.

Example:

4>>2 here 4 will be converted to binary (0100), and 
     all the bits will move 2 bits right.

Results in → 0001(1 in decimal)

Code:

Java Code

class Bitwise {
    public static void main(String[] args) {
        int num = 4;
        int n = 2;
        System.out.println(s_left(num,n));
    }

    private static int s_left(int a,int n) {
        //Shift right
        int ans = a>>n;
        return ans;
    }
}

Output: 1

Unsigned right operator:

It moves the bits of the given number to a specified number of places to the right. The sign bit was filled with 0’s, It does not preserve the sign bit.

Right signed and unsigned provide different values for negative bits.

Symbol: >>> 

num >>> n

Code:

Java Code

 class Bitwise {
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = -10;
        int n = 2;
        System.out.println(num1 >>> 2);
        System.out.println(num2 >>> 2);
    }
 }

Output:

2
1073741821

Unsigned Left Shift:

Java doesn’t support unsigned left shift (<<<) because both signed and unsigned left shift operators to work identically.

Special thanks to Sai bargav Nellepalli for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article