LeetCampus
Interview Question

What is the difference between ‘>>’ and ‘>>>’ operators in java?

July 24, 2025
0 views
Difficulty: Medium
Popularity: Common
Share on

Question Explanation

The question seeks to clarify the difference between the shift operators >> and >>> in Java. Interviewers often ask this to assess a candidate's understanding of bitwise operations and how Java handles signed vs. unsigned shifts. The >> operator is known as the right shift operator, which preserves the sign bit of the number, while the >>> operator is the unsigned right shift operator, which fills the leftmost bits with zeros regardless of the sign. Understanding these operators is crucial for tasks involving low-level data manipulation, performance optimization, and systems programming. Candidates may confuse these operators due to their similar appearances but different functionalities. This knowledge is applicable in optimizing algorithms where bitwise manipulation is essential, especially in areas like cryptography, graphics, and network programming.

Sample Answers

Example 1: Understanding the >> Operator

The >> operator is a signed right shift operator in Java. When you use this operator, it shifts the bits of a number to the right while preserving the sign bit (the leftmost bit). This means if the number is positive, it fills the new bits with zeros; if negative, it fills them with ones. For example, consider the following code:

int num = -8; // Binary: 11111111 11111111 11111111 11111000
int result = num >> 2; // Result: 11111111 11111111 11111111 11111110

In this case, the result will still be negative. The sign extension is crucial in maintaining the value of the number during the shift. Keep in mind that this operator can lead to unexpected results for negative numbers, so it’s essential to understand the implications of sign bits.

Example 2: Exploring the >>> Operator

The >>> operator is the unsigned right shift operator. Unlike the >>, this operator shifts the bits to the right and fills the leftmost bits with zeros, regardless of the sign of the number. This is especially useful when working with binary data where the sign is not relevant. For instance, consider:

int num = -8; // Binary: 11111111 11111111 11111111 11111000
int result = num >>> 2; // Result: 00111111 11111111 11111111 11111110

In this example, shifting -8 results in a positive number, showing how the >>> operator can be advantageous when you need a non-negative result from a shift operation. It's important to note that this operator is often used in scenarios where the data is treated as binary rather than as a signed integer.

Example 3: Practical Applications and Performance Considerations

Understanding the difference between >> and >>> is not just theoretical; it has practical applications in performance-critical code. For instance, in graphics programming, you may need to manipulate pixel data directly. Using the right operator can ensure that the data is processed correctly. Here's a scenario:

int color = -1; // Represents a color in ARGB format
int red = (color >> 16) & 0xFF; // Signed right shift to extract red
int green = (color >> 8) & 0xFF; // Signed right shift to extract green
int blue = color & 0xFF; // No shift needed for blue

In this case, using >> allows for proper extraction of color values. However, if we needed to manipulate these values without considering the sign, >>> would be the appropriate choice. Understanding these nuances is vital for optimizing algorithms and ensuring accurate data processing in applications.

Keywords

Javabitwise operatorssigned shiftunsigned shiftsoftware engineering

Ready to practice more questions?

Explore our collection of technical interview questions from top companies.

View All Questions