What does the bitwise right shift operator (>>) do? Give an example.

Quick Overview

Explain what the bitwise right shift operator does to the bits of an integer and work through an example. Tests binary representation, arithmetic versus logical shifts, how negative numbers round compared with integer division, and how the rules differ across languages.

What does the bitwise right shift operator (>>) do? Give an example.

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

What does the bitwise right shift operator (`>>`) do? Give an example. Right shift behaves differently for signed and unsigned values, so be ready to explain what happens to negative numbers as well as positive ones. ```hint Think in binary Write 10 in binary and slide every bit to the right. Then consider what should fill the empty positions on the left when the number is negative. ``` ### What a Strong Answer Covers - What happens to the bit pattern: which way the bits move, what happens to the low bits, and what fills the high positions. - What shifting right by k positions means arithmetically, shown on a worked example. - The difference between arithmetic and logical right shift, and which one common languages use for signed and unsigned values. - How right shift rounds negative numbers compared with integer division. - At least one practical use, such as reading a single bit or extracting a field. ### Follow-up Questions - In Java, why does `-7 >> 1` give -4 while `-7 / 2` gives -3? - What does Java's `>>>` do, and why does `(lo + hi) >>> 1` give a correct midpoint even when `lo + hi` overflows? - Why can `while (x != 0) x >>= 1;` loop forever, and how would you fix it?

Overview: Explain what the bitwise right shift operator does to the bits of an integer and work through an example. Tests binary representation, arithmetic versus logical shifts, how negative numbers round compared with integer division, and how the rules differ across languages.

PracHub logo
General
Nov 25, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

What does the bitwise right shift operator (>>) do? Give an example.

Right shift behaves differently for signed and unsigned values, so be ready to explain what happens to negative numbers as well as positive ones.

What a Strong Answer Covers Guidance

  • What happens to the bit pattern: which way the bits move, what happens to the low bits, and what fills the high positions.
  • What shifting right by k positions means arithmetically, shown on a worked example.
  • The difference between arithmetic and logical right shift, and which one common languages use for signed and unsigned values.
  • How right shift rounds negative numbers compared with integer division.
  • At least one practical use, such as reading a single bit or extracting a field.

Follow-up Questions Guidance

  • In Java, why does -7 >> 1 give -4 while -7 / 2 gives -3?
  • What does Java's >>> do, and why does (lo + hi) >>> 1 give a correct midpoint even when lo + hi overflows?
  • Why can while (x != 0) x >>= 1; loop forever, and how would you fix it?