Java ProgramsBasics & I/ODemonstrate Right Shift

Demonstrate Right Shift in Java

beginner·  Basics & I/O  ·  Operators

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.

Input
-8, 1
Output
-8 >> 1 = -4

Java Program

Java
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

a = -8 (11111111111111111111111111111000) a >> 1 = -4 (11111111111111111111111111111100)

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.

How It Works
  1. 1a's 32-bit two's complement form starts with a 1, since a is negative.
  2. 2a >> 1 moves every bit one position to the right, dropping the rightmost bit, and fills the newly-vacated leftmost position with another 1 — a copy of the original sign bit, not a plain 0.
  3. 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. 4-8 >> 1 gives -4, the same result as -8 / 2.
For 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

>> operatorarithmetic shiftsign extension

Approach 2: Java 8

Java
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

a = -8 (11111111111111111111111111111000) a >> 1 = -4 (11111111111111111111111111111100)

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.

How It Works
  1. 1IntBinaryOperator rightShift = (x, y) -> x >> y; stores the signed right-shift logic as a lambda.
  2. 2rightShift.applyAsInt(a, shiftBy) calls it with a and shiftBy, returning the same result a >> shiftBy would inline, sign bit copied in exactly as before.
  3. 3The 32-bit binary strings are built exactly as before, padded with String.format().
With 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.

Key Concepts

IntBinaryOperatorfunctional interfacelambda expression

Related Programs