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 = 12How << Works
- Take the source value in binary form.
- Move bits left by the shift count.
- Fill empty right-side bits with 0.
- 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 << 1is usually same asvalue * 2.value << 2is usually same asvalue * 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 << 7in an 8-bit value is valid.1 << 8goes 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
stepIndexcontrols how far bits are shifted.shiftedValue = 1 << stepIndexcreates 1, 2, 4, 8 pattern.multipliedValueis printed to compare with normal arithmetic thinking.statusFlags = statusFlags | shiftedValueaccumulates shifted bits.if (shiftedValue & ledMask)checks whether the active shifted bit matches the LED mask.- Serial output prints binary values so students can see exact bit movement.
- When
stepIndexpasses 3, the loop resets to repeat the learning cycle.
What Happens Inside
- CPU reads source bits from memory/register.
- Shift unit moves bits left by shift count.
- New low bits are filled with zero.
- Result bits are stored into destination variable.
- Subsequent bitwise checks read this new value for logic decisions.
| Expression | Binary | Decimal |
|---|---|---|
1 << 0 | 00000001 | 1 |
1 << 1 | 00000010 | 2 |
1 << 2 | 00000100 | 4 |
1 << 3 | 00001000 | 8 |
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.