Demonstrate Right Shift in Java
Problem
The right shift operator (>>) moves every bit of a number to the right by a given count, filling the vacated positions on the left with copies of the original sign bit — so a negative number stays negative after shifting.
Given a negative integer, shift its bits right by a given count and show that the sign is preserved.
Java Program
public class DemonstrateRightShift {
public static void main(String[] args) {
int a = -8;
int shiftBy = 1;
int result = a >> shiftBy; // sign bit is copied into the vacated left positions, so a negative stays negative
System.out.println("a = " + a + " (" + String.format("%32s", Integer.toBinaryString(a)).replace(' ', '0') + ")");
System.out.println("a >> " + shiftBy + " = " + result + " (" + String.format("%32s", Integer.toBinaryString(result)).replace(' ', '0') + ")");
}
}Output
Core Logic
Shifting a's bits right by 1 position fills the newly-vacated leftmost position with the same sign bit a already had, which is why a negative input always produces a negative result.
- 1
a's 32-bit two's complement form starts with a1, sinceais negative. - 2
a >> 1moves every bit one position to the right, dropping the rightmost bit, and fills the newly-vacated leftmost position with another1— a copy of the original sign bit, not a plain0. - 3This is called sign extension: it's what keeps a negative number negative after a right shift, matching the mathematical result of dividing by 2 and rounding toward negative infinity.
- 4
-8 >> 1gives-4, the same result as-8 / 2.
a = -8: the sign bit 1 is copied into the vacated leftmost position, producing the bit pattern for -4.Key Point: >> always preserves the sign, which is exactly why it differs from >>> (unsigned right shift) whenever the operand is negative — for positive numbers, the two operators behave identically.
Key Concepts
Approach 2: Java 8
import java.util.function.IntBinaryOperator;
public class RightShiftLambda {
public static void main(String[] args) {
int a = -8;
int shiftBy = 1;
// The >> logic is stored as a named, reusable lambda
IntBinaryOperator rightShift = (x, y) -> x >> y;
int result = rightShift.applyAsInt(a, shiftBy);
System.out.println("a = " + a + " (" + String.format("%32s", Integer.toBinaryString(a)).replace(' ', '0') + ")");
System.out.println("a >> " + shiftBy + " = " + result + " (" + String.format("%32s", Integer.toBinaryString(result)).replace(' ', '0') + ")");
}
}
Output
Core Logic
Wrapping >> in a named IntBinaryOperator turns 'shift this int right by this many bits' into a reusable value instead of a one-off inline expression.
- 1
IntBinaryOperator rightShift = (x, y) -> x >> y;stores the signed right-shift logic as a lambda. - 2
rightShift.applyAsInt(a, shiftBy)calls it withaandshiftBy, returning the same resulta >> shiftBywould inline, sign bit copied in exactly as before. - 3The 32-bit binary strings are built exactly as before, padded with
String.format().
a = -8 and shiftBy = 1, rightShift.applyAsInt(-8, 1) returns -4, same as a >> shiftBy.Key Point: Naming the lambda rightShift makes it clear at the call site what operation it performs — the same pattern used for the arithmetic and left-shift pages.