Operators

Lesson 54: Using << Bitshift Left Operator

Learn how << shifts bits to the left, scales values by powers of two, and supports mask-based embedded logic.

Progress indicator

Lesson 54 of 57

Learning Objectives

  • Understand what left shift (<<) does at bit level.
  • Predict results of 1 << n quickly without calculator.
  • Compare << with multiply-by-2 behavior and limits.
  • Use << with masks and flags in embedded-style logic.
  • Avoid overflow and data-width mistakes while shifting.

Concept Explanation

What is the Left Shift Operator (<<)

The << operator moves every bit of a number to the left.

The number of positions moved is called the shift count.

Left Shift Syntax

result = value << shiftCount;
int x = 3 << 2; // x = 12

How << Works

  1. Take the source value in binary form.
  2. Move bits left by the shift count.
  3. Fill empty right-side bits with 0.
  4. Save the final bit pattern as result.

Bit Movement Concept

1      = 00000001
1 << 1 = 00000010 (2)
1 << 2 = 00000100 (4)
1 << 3 = 00001000 (8)

Real-life analogy: think of moving one lit LED indicator one slot left each step.

<< vs Multiply by 2 (Comparison)

  • value << 1 is usually same as value * 2.
  • value << 2 is usually same as value * 4.
  • For large values, both can overflow the same type limit.

Using << with Bit Masks

1 << n builds a mask where only bit n is ON.

byte mask = 1 << 3; // 00001000
if (flags & mask) {
  // bit 3 is set
}

Data Width and Overflow

Shifts depend on variable size. For example, byte is 8 bits. If you shift too far, useful bits can be lost.

  • 1 << 7 in an 8-bit value is valid.
  • 1 << 8 goes beyond 8-bit range and is not useful for byte masks.
  • Prefer clear type choice before shifting.

When to Use <<

  • Creating bit masks dynamically.
  • Working with packed flags and register-style settings.
  • Power-of-two scaling in low-level code.

Example Code

This sketch uses 1 << stepIndex to generate a moving bit pattern and compares shift output with arithmetic scaling.

const int LED_PIN = 2;

byte ledMask = 0b00000001;
byte statusFlags = 0;
int stepIndex = 0;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  int shiftedValue = 1 << stepIndex;
  int multipliedValue = 1 * (1 << stepIndex);

  statusFlags = statusFlags | shiftedValue;

  if (shiftedValue & ledMask) {
    digitalWrite(LED_PIN, HIGH);
  } else {
    digitalWrite(LED_PIN, LOW);
  }

  Serial.print("stepIndex=");
  Serial.print(stepIndex);
  Serial.print(", shiftedValue(bin)=");
  Serial.print(shiftedValue, BIN);
  Serial.print(", multipliedValue=");
  Serial.print(multipliedValue);
  Serial.print(", statusFlags=");
  Serial.println(statusFlags, BIN);

  delay(500);

  stepIndex = stepIndex + 1;
  if (stepIndex > 3) {
    stepIndex = 0;
    statusFlags = 0;
  }
}

Example Code Explanation

  1. stepIndex controls how far bits are shifted.
  2. shiftedValue = 1 << stepIndex creates 1, 2, 4, 8 pattern.
  3. multipliedValue is printed to compare with normal arithmetic thinking.
  4. statusFlags = statusFlags | shiftedValue accumulates shifted bits.
  5. if (shiftedValue & ledMask) checks whether the active shifted bit matches the LED mask.
  6. Serial output prints binary values so students can see exact bit movement.
  7. When stepIndex passes 3, the loop resets to repeat the learning cycle.

What Happens Inside

  1. CPU reads source bits from memory/register.
  2. Shift unit moves bits left by shift count.
  3. New low bits are filled with zero.
  4. Result bits are stored into destination variable.
  5. Subsequent bitwise checks read this new value for logic decisions.
ExpressionBinaryDecimal
1 << 0000000011
1 << 1000000102
1 << 2000001004
1 << 3000010008

Common Mistakes with <<

  • Using too large shift counts for the data type width.
  • Confusing << with stream operators in other languages.
  • Assuming shift always safe without overflow checking.

Best Practices for <<

  • Use named constants for bit positions.
  • Prefer unsigned types for bit-mask logic when possible.
  • Print binary output during learning and debugging.
  • Keep shift operations simple and readable in one line.

Try it now

Open the simulator workspace and inspect left-shift bit changes step-by-step.

Run in Simulator