Java - Bitwise Operators

Java

In this tutorial we will learn about bitwise operators in Java programming language.

We use the bitwise operators to work with bits i.e., 0s and 1s.

Following are the bitwise operators that we can use in Java.

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise exclusive OR i.e. XOR
~One's complement
<< Shift left
>> Shift right
>>> Shift right with zero fill

Bitwise operator may not be applied to float and double.

Bitwise AND

Bitwise AND & will give 1 only if both the operands are 1 otherwise, 0.

The truth table for bitwise AND.

ABA & B
000
010
100
111

In the following example we have two integer values 1 and 2 and we will perform bitwise AND operation and display the result.

class Bitwise {
  public static void main(String args[]) {
    //declare variables
    int x = 1;
    int y = 2;

    System.out.println("Result: " + x + " & " + y + " = " + (x & y));
  }
}

Output:

Result: 1 & 2 = 0

Calculation of the bitwise AND operation for the above code.

// representing decimal in 32 bits (4 bytes)

  1 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0001 (binary)
& 2 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0010 (binary)
----------------------------------------------------------------
  0 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0000 (binary)

Click here to learn how to convert numbers from decimal to binary.

Bitwise OR

Bitwise OR | will give 0 only if both the operands are 0 otherwise, 1.

The truth table for bitwise OR.

ABA | B
000
011
101
111

In the following example we will perform bitwise OR operation and display the result using two integer values 1 and 2.

class Bitwise {
  public static void main(String args[]) {
    //declare variables
    int x = 1;
    int y = 2;

    System.out.println("Result: " + x + " | " + y + " = " + (x | y));
  }
}

Output:

Result: 1 | 2 = 3

Calculation of the bitwise OR operation for the above code.

// representing decimal in 32 bits (4 bytes)

  1 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0001 (binary)
| 2 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0010 (binary)
----------------------------------------------------------------
  3 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0011 (binary)

Bitwise XOR

Bitwise XOR ^ will give 1 for odd number of 1s otherwise, 0.

The truth table for bitwise XOR.

ABA ^ B
000
011
101
110

In the following example we have two integer values 2 and 7 and we will perform bitwise XOR operation and display the result.

class Bitwise {
  public static void main(String args[]) {
    //declare variables
    int x = 2;
    int y = 7;

    System.out.println("Result: " + x + " ^ " + y + " = " + (x ^ y));
  }
}

Output:

Result: 2 ^ 7 = 5

Calculation of the bitwise XOR operation for the above code.

// representing decimal in 32 bits (4 bytes)

  2 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0010 (binary)
^ 7 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0111 (binary)
----------------------------------------------------------------
  5 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0101 (binary)

One's complement

One's complement ~ is a unary operator and it converts 0s to 1s and 1s to 0s.

In the following example we an integer values 2 and we will compute its one's complement.

class Bitwise {
  public static void main(String args[]) {
    //declare variables
    int x = 2;

    System.out.println("Result: ~" + x + " = " + (~x));
  }
}

Output:

Result: ~2 = -3

Calculation for the above code.

// representing decimal in 32 bits (4 bytes)

2 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0010 (binary)
         ~2 = 1111 1111 1111 1111 1111 1111 1111 1101 (binary)
            = -3 (decimal)

We get -3 because we are working with signed integer number.

Note! Data type int has a range -2,147,483,648 to 2,147,483,647. Click here for Java data type tutorial.

Shift Left

We use shift left << operator to shift the bits left.

In the following example we have an integer which we will left shift 1 position.

class Bitwise {
  public static void main(String args[]) {
    //declare variables
    int x = 4;

    System.out.println("Result: " + x + " << 1 = " + (x << 1));
  }
}

Output

Result: 4 << 1 = 8

Calculation:

// representing decimal in 32 bits (4 bytes)

4 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0100 (binary)
--------------------------------------------------------------
     4 << 1 = 0000 0000 0000 0000 0000 0000 0000 1000 (binary)
            = 8 (decimal)

Shift Right

We use shift right >> operator to shift the bits right.

In the following example we have an integer which we will right shift 1 position.

class Bitwise {
  public static void main(String args[]) {
    //declare variables
    int x = 4;

    System.out.println("Result: " + x + " >> 1 = " + (x >> 1));
  }
}

Output

Result: 4 >> 1 = 2

Calculation:

// representing decimal in 32 bits (4 bytes)

4 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0100 (binary)
--------------------------------------------------------------
     4 >> 1 = 0000 0000 0000 0000 0000 0000 0000 0010 (binary)
            = 2 (decimal)

Shift Right with zero fill

We use shift right with zero fill >>> operator to shift the bits right and fill the left side with zeros.

In the following example we have an integer which we will right shift with zero fill 1 position.

class Bitwise {
  public static void main(String args[]) {
    //declare variables
    int x = -1;

    System.out.println("Result: " + x + " >>> 1 = " + (x >>> 1));
  }
}

Output

Result: -1 >>> 1 = 2147483647

Calculation:

// representing decimal in 32 bits (4 bytes)

-1 (decimal) = 1111 1111 1111 1111 1111 1111 1111 1111 (binary)
---------------------------------------------------------------
    -1 >>> 1 = 0111 1111 1111 1111 1111 1111 1111 1111 (binary)
             = 2147483647 (decimal)

Difference between Shift Right >> and Shift Right with zero fill >>>

Both the operators fill the leftmost bit with 0 when shifting the bits to the right side when working with positive numbers.

But the difference arises with negative numbers.

For negative numbers the leftmost bit is 1.

So, when we use the shift right >> operator then the leftmost bit is preserved as 1.

Whereas, when we use the shift right with zero fill >>> operator then the leftmost but is set to 0 thus converting a negative number into positive number.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT