What does the bitwise left shift operator (<<) do? Give an example.

Quick Overview

Explain what the bitwise left shift operator does to the bits of an integer and work through an example. Tests binary representation, the link between shifting and powers of two, and how overflow and shift-count rules differ across fixed-width languages and Python.

What does the bitwise left shift operator (<<) do? Give an example.

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

What does the bitwise left shift operator (`<<`) do? Give an example. Shifts come up whenever a problem involves bit masks, flags or powers of two, so be ready to explain both what happens to the bits and what happens to the value. ```hint Think in binary Write a small number in binary, move every bit one place to the left, and compare the new value with the old one. ``` ### What a Strong Answer Covers - What happens to the bit pattern: which way the bits move and what fills the vacated positions. - What shifting left by k positions means arithmetically, shown on a worked example. - What happens when bits move past the width of a fixed-size integer, and how signed values behave. - How the rules differ between fixed-width languages such as Java or C and Python's unbounded integers. - At least one practical use, such as building a mask for a single bit. ### Follow-up Questions - How would you build a mask with only bit i set, and use it to test, set and clear that bit? - In Java, what are `1 << 31`, `1 << 32` and `1 << 40` for an `int`, and how do you get a 64-bit result? - How does the right shift operator behave on negative numbers, and what does Java's `>>>` change?

Overview: Explain what the bitwise left shift operator does to the bits of an integer and work through an example. Tests binary representation, the link between shifting and powers of two, and how overflow and shift-count rules differ across fixed-width languages and Python.

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

What does the bitwise left shift operator (<<) do? Give an example.

Shifts come up whenever a problem involves bit masks, flags or powers of two, so be ready to explain both what happens to the bits and what happens to the value.

What a Strong Answer Covers Guidance

  • What happens to the bit pattern: which way the bits move and what fills the vacated positions.
  • What shifting left by k positions means arithmetically, shown on a worked example.
  • What happens when bits move past the width of a fixed-size integer, and how signed values behave.
  • How the rules differ between fixed-width languages such as Java or C and Python's unbounded integers.
  • At least one practical use, such as building a mask for a single bit.

Follow-up Questions Guidance

  • How would you build a mask with only bit i set, and use it to test, set and clear that bit?
  • In Java, what are 1 << 31 , 1 << 32 and 1 << 40 for an int , and how do you get a 64-bit result?
  • How does the right shift operator behave on negative numbers, and what does Java's >>> change?