Select Page

Java Bitwise and Shift Operators

Robert Gravelle
Published: December 17, 2022

Java Programming tutorials

Operators are used in programming languages to perform operations on data and variables. The Java programming language include many types of operators, including these, which we recently explored:

This programming tutorial will cover everything developers need to know regarding Bitwise and Shift operators, which perform operations at the bit level of a number.

Bitwise Operators in Java

As mentioned in the introduction, Bitwise operators can be used with any integral (i.e. whole number) type. These include long, int, short, char, and byte. The Bitwise operators consist of:

  • & – performs a bitwise AND operation
  • | – performs a bitwise inclusive OR operation
  • ^ – performs a bitwise exclusive OR (XOR) operation
  • ~ – performs a bitwise complement operation

Bitwise operators work on a binary equivalent of decimal numbers and perform operations on them bit by bit according to the following process:

  1. First, the operands are converted to their binary representation.
  2. Next, the operator is applied to each binary number and the result is calculated.
  3. Finally, the result is converted back into a decimal format.

Now let’s take a look at how to use each of the above Java Bitwise operators and see what they do.

Bitwise AND (&) Java Examples

The bitwise AND operator is denoted by the ampersand (&) symbol. It returns 1 if – and only if – both bits are 1, else it returns 0.

Here is a table that shows the binary representations of two int variables where a = 9 and b = 8, as well as the resulting binary number after applying the Bitwise AND operator:

aba & b
000
010
100
111

Once converted back to a decimal number, we get a value of 8, as seen in the following example code:

public class BitwiseAndExample {

  public static void main(String[] args) {
    int a = 9, b = 8;   
		
    // Outputs "a & b = 8"
    System.out.println("a & b = " + (a & b));
  }
}

Read: Java Tools to Increase Productivity

Bitwise OR (|) Java Example

The Bitwise OR operator employs the familiar pipe (|) character. It returns 1 if either of the bits is 1, otherwise, it returns 0.

Here is a table that shows the binary representations of the same two int variables as before (where a = 9 and b = 8), along with the resulting binary number after applying the Bitwise OR operator:

aba | b
000
011
101
111
public class BitwiseOrExample {   

  public static void main(String[] args) {   
    int a = 9, b = 8;   
    // Outputs "a | b = 9"
    System.out.println("a | b = " + (a | b));   
  }  
}  

Bitwise XOR (^) Java Example

The Bitwise XOR operator utilizes the caret (^) symbol. It returns 0 if both bits are the same, otherwise it returns 1.

After applying the Bitwise XOR operator, our int variables of 8 and 9 become 0110, or 1:

aba ^ b
000
011
101
110
public class BitwiseXorExample {   
  public static void main(String[] args) {   
    int a = 9, b = 8;   
    // Outputs "a ^ b = 1
    System.out.println("a ^ b = " + (a ^ b));   
  }  
}  

Bitwise Complement (~) Java Example

Unlike the previous three Bitwise operators, the Bitwise Complement is a unary operator, meaning that it works with only one operand, as opposed to two. It employs the tilde (~) symbol and returns the inverse or complement of the bit. Hence, it makes every 0 a 1 and every 1 a 0. The bitwise complement of any integer N is equal to - (N + 1), so we can use that formula to calculate what the result of the Bitwise Complement will be on a number. For example, if we have an integer of 35, the bitwise complement of 35 should be -(35 + 1), or -36. Here is some example code that confirms our supposition:

public class BitwiseComplimentExample {   
  public static void main(String[] args) {   
    int x = 35;   
    // Outputs "~x = -36
    System.out.println("~x = " + (~x));   
  }  
}   

Java Shift Operators

Shift operators in Java are used to shift the bits of a number either right or left. Programmers can use shift operators if we divide or multiply any number by 2. There are three types of shift operators in Java:

  • Signed Left Shift (<<)
  • Signed Right Shift (>>)
  • Unsigned Right Shift (>>>)

The general format to shift the bit is:

variable << or >> number of places to shift;  

Signed Left Shift Java Example

The left shift operator moves all bits by a given number of bits to the left. Here is some example code to demonstrate:

class SignedLeftShiftExample {
  public static void main(String[] args)
  {
    byte a = 64;
    int i = a << 2;
    int b = (byte)(a << 2);
    System.out.println("Original value of a: " + a); // Original value of a: 64
    System.out.println("i and b: " + i + ", " + b);   // i and b: 256, 0
  }
}

Shifting the value of a number to the left two places causes the leftmost two bits to be lost. The binary representation of the number 2 is 0010.

Signed Right Shift Java Example

The Right Shift operator moves the bits of a number in a given number of places to the right. Shifting a number to the right causes the least significant (rightmost) bits to be deleted, and the sign bit is filled in the most significant (leftmost) position. In the code example below, the binary number 1000 (in decimal 8) becomes 0010 after shifting the bits to the right (in decimal 2):

class SignedRightShiftExample {
  public static void main(String[] args) {
    int number = 8;
       
    // 2 bit signed right shift
    int result = number >> 2;
    
    System.out.println(result); // 8
  }
}

Unsigned Right Shift Java Example

The Unsigned Right Shift operator moves the bits of the integer a given number of places to the right. The sign bit is filled with 0s. Here is an example showing how to use Java’s Unsigned Right Shift operator:

class UnsignedRightShiftExample {
  public static void main(String[] args) {
    byte num1 = 8;
    byte num2 = -8;
    
    System.out.println(num1 >>> 2); // 2
    System.out.println(num2 >>> 2); // 1073741822   
  }
}

Note that there is no Unsigned Left Shift (<<<) operator in Java because the logical (<<) and arithmetic left-shift (<<<) operations are identical.

Final Thoughts on Java Bitwise and Shift Operators

In this programming tutorial we learned all about Bitwise operators and Shift operators, which both perform operations at the bit level of a number. Though not as extensively utilized as other Java operators, some potential use cases of bitwise operators are:

  • In digital messages where the individual bits in the header contain important information.
  • In embedded systems to modify a single bit without altering the remaining bits.
  • To encrypt sensitive data.
  • In data compression algorithms whose aim is to reduce the amount of space used.

Read more Java programming tutorials and software development guides.

Source: www.developer.com