Left shift << mechanics
What you'll learn: How the left shift operator slides bits to the left to perform lightning-fast multiplication by powers of two.
Sliding the bits
The left shift operator (<<) takes the binary representation of a number and moves every bit to the left by a specific number of positions. Think of it like a row of people sitting in a theater. If the usher says, "Everyone move two seats to the left," everyone shifts over, the people at the very far left end fall out of the row (into the "bit bucket"), and empty seats open up on the far right.
In C, those empty seats on the right are always filled with zeros.
The "Doubling" effect
The most powerful way to visualize this is through place values. In our standard base-10 system, shifting digits left (e.g., turning 5 into 50) multiplies the number by 10. Because binary is base-2, shifting a bit one position to the left effectively doubles its value.
5 << 1becomes105 << 2becomes205 << 3becomes40
Mathematically, x << n is equivalent to .
See it in code
Here is how you apply the operator in a C program. Notice how the bits physically migrate toward the higher-value positions.
#include <stdio.h>
int main() {
// Binary: 0000 0101 (which is 5)
unsigned char myNumber = 5;
// Shift left by 2 positions
// The 1s move from the 1 and 4 spots to the 4 and 16 spots
// Result: 0001 0100 (which is 20)
unsigned char result = myNumber << 2;
printf("Original: %d\n", myNumber); // Output: 5
printf("Shifted: %d\n", result); // Output: 20
return 0;
}
Watch the overflow
Computers have a limited number of "seats" (bits). If you shift a bit too far to the left, it moves past the boundary of the variable's size (like an int or char) and is lost forever. This is called overflow. If you shift a 1 past the edge, your number might suddenly become much smaller or even zero because the significant data "fell off" the end.
Key takeaway
The << operator moves bits left and fills the right side with zeros, effectively multiplying a number by 2 for every position shifted.