Left Shift Operator in Java


The execution of instructions in programming languages involves the operation of various symbols referred to as "operators." Operators tell computers what action/value evaluation should be performed throughout set codes. Arithmetic-based operates consist of performing essential calculations like addition/subtraction/multiplication/division. Played out with relational-oriented operates that indicate interactions between any two values and analyzes when one is greater/equal/less with regards to another value - showing its relevance under certain conditions. Logical-operates' primary role often includes merging diverse quality statements into significant arguments either as True/False statement criteria based on developers' intentions efficiently using all three indispensable operator types.

Left Shift Operator in Java

Java is a powerful language and provides a great range of operators, one of which is a left-shift operator which lends a great hand in shifting a number by a certain number of positions. This operator is not only used for shifting numbers but can also be employed for shifting strings, meaning it allows for more complex operations. As different languages employ different shift operations and behaviors, it is important to identify how the concept of shifting applies to Java. As such, one must understand the perils and benefits of this operator.

Syntax

The syntax for this operator goes as follows −

X<<n
Here,
x: an integer
n: a non-negative integer

Return type

A number after x has been moved left by n places.

Exception

The output is undefined when n is negative

Representation

Decimal Representation − The base -10 number system, which only has ten states of 0, 1,2,3,4,5,6,7,8, and 9, is used to represent numbers in decimal form. For example, 4, 10, 16 etc.

Binary Representation − A number is represented in binary form using a base-2 number system with just the states 0 and 1. for instance, the base-9 decimal numeral system gives the binary equivalent of 4 as 100, 10 as 1010, 16 as 1000, etc.

Left Shift

The left shift indicates a leftward shift of each bit in binary encoding.

Left shifting is an essential process when operating with binary numbers. For example, shifting the number 5 to the left by one position yields 10, or 1010. Similarly, shifting 10 by two positions to the left renders 40, or 101000.

But this process does more than just alter the number. In essence, it is shifting each bit by one position towards the left. Thus, a greater degree of complexity and variation is introduced that would not be present if the number was just shifted. This leads to an increased level of perplexity and burliness, making the operation more interesting.

Left shift x by n positions - x*2n

Examples

Example 1

// Java program to illustrate the
// working of left shift operator


import java.io.*;


public class example {
   // Main method
   public static void main (String[] args) {
		
      // Number to be shifted
      int x = 5;
		
      // Number of positions
      int n = 1;
		
      // Shifting x by n positions towards left using left shift operator
      int answer = x << n;
		
      // Print the number obtained after shifting x by n positions towards left
      System.out.println("Left shift " + x + " by " + n + " positions: " + answer);
		
      // Number to be shifted
      x = answer;
		
      // Number of positions
      n = 2;
		
      // Shifting x by n positions towards left using left shift operator
      answer = answer << n;
		
      // Print the number obtained after shifting x by n positions towards left
      System.out.println("Left shift " + x + " by " + n + " positions: " + answer);
		
   }
}

Output

Left shift 5 by 1 positions: 10
Left shift 10 by 2 positions: 40

Example 2

// Java program to illustrate the
// working of left shift operator

import java.io.*;

public class example {

   // Main method
   public static void main (String[] args) {
		
      // Number to be shifted
      int x = -2;
		
      // Number of positions
      int n = 1;
		
      // Shifting x by n positions towards
      // left using left shift operator
      int answer = x << n;
		
      // Print the number obtained after shifting x by n positions towards left
      System.out.println("Left shift " + x + " by " + n + " positions: " + answer);
		
      // Number to be shifted
      x = answer;
		
      // Number of positions
      n = 2;
		
      // Shifting x by n positions towards
      // left using left shift operator
      answer = answer << n;
		
      // Print the number obtained after shifting x by n positions towards left
      System.out.println("Left shift " + x + " by " + n + " positions: " + answer);
		
	}
}

Output

Left shift -2 by 1 positions: -4
Left shift -4 by 2 positions: -16
  • Time complexity − O(1)

  • Space complexity − O(1)

Note − Performing an arithmetic left shift requires filling the right-most vacant bits with 0s, as this does not change the sign of the number, meaning that the sign bit is not taken into consideration during the shift. This means that this type of left shift has the same outcome as a logical (unsigned) one, rendering the need for a separate unsigned shift operator unnecessary.

Conclusion

Operators represent key symbols or characters utilized in coding to indicate what process should be executed. Typically divided into three main groups -arithmetic-, relational- and logical- each serves an essential role in programming operations. An example of these is the left-shift operator which proves valuable in shifting numbers by a specified number of positions as well as handling more intricate jobs on strings.

Updated on: 01-Aug-2023

406 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements