- How the
<<operator shifts bits to the left - Why
n << kis equivalent ton × 2k - How bits shifted off the left edge are lost (overflow)
- How bits are filled with zeros on the right
- How to use left shift for fast multiplication by powers of 2
The left shift operator
The bitwise left shift operator << takes two operands: the value to shift and the number of positions to shift. n << k moves every bit of n to the left by k positions, filling the vacated low-order bits with zeros. Bits that are shifted off the left edge are discarded (lost).
In other words: each left shift by 1 doubles the value. 5 << 1 = 10, 5 << 2 = 20, 5 << 3 = 40. This is because each bit in n moves to a position worth twice as much — bit k (worth 2k) becomes bit k+1 (worth 2k+1).
Common uses of <<:
- Fast multiplication —
n << 3is faster thann * 8on some architectures (modern compilers often make this optimization automatically). - Setting a bit —
1 << kproduces a mask with only bitkset. - Building bit patterns — construct flags, colors, or packed fields.
- Powers of 2 —
1 << nis the nth power of 2.
n << k shifts bits left by k positions, equivalent to multiplying by 2k. Bits that fall off the left are lost; zeros fill in on the right.
The multiply-by-2n rule
For non-negative n and a shift count k such that n × 2k doesn't overflow the type, the identity is:
n << k == n × 2k
Examples:
5 << 0 = 5 × 20 = 5 × 1 = 55 << 1 = 5 × 21 = 5 × 2 = 105 << 2 = 5 × 22 = 5 × 4 = 205 << 3 = 5 × 23 = 5 × 8 = 401 << 10 = 1 × 210 = 1024
This identity is why left shift is often called "multiply by power of 2." It's exact for non-negative integers and doesn't overflow. For negative numbers, the behavior is implementation-defined before C99 (arithmetic vs. logical shift of the sign bit), but modern C compilers follow two's complement — −5 << 1 = −10, consistent with the multiply rule.
Shift example with binary representation — 5 << 2:
Each shift moves the bits one position to the left and fills the rightmost bit with 0. The value doubles with each shift.
Complete C Program
Here is the program — left shift an integer by a specified count:
Program: Left shift an integer
#include <stdio.h>
int main(void) {
int n, k;
printf("Enter a number and a shift count: ");
scanf("%d %d", &n, &k);
if (k < 0 || k >= 32) {
printf("Error: shift count must be between 0 and 31\n");
return 1;
}
printf("%d << %d = %d\n", n, k, n << k);
return 0;
}
The variant with binary display makes the operation visible:
Variant: Show binary representation
#include <stdio.h>
void print_binary(int n) {
for (int i = 7; i >= 0; i--) {
printf("%d", (n >> i) & 1);
if (i % 4 == 0 && i != 0) printf(" ");
}
printf("\n");
}
int main(void) {
int n, k;
printf("Enter a number (0-255) and shift count (0-7): ");
scanf("%d %d", &n, &k);
if (k < 0 || k > 7) {
printf("Error: shift count must be between 0 and 7\n");
return 1;
}
printf("\nn = ");
print_binary(n);
printf("n << %d = ", k);
print_binary(n << k);
printf("\nDecimal result: %d\n", n << k);
return 0;
}
The print_binary helper iterates from bit 7 down to bit 0, using (n >> i) & 1 to isolate each bit. This displays the binary representation before and after the shift, making the "left shift = multiply by 2" rule visually obvious.
Program Output
Sample run of the basic program:
Enter a number and a shift count: 5 2
5 << 2 = 20
5 × 22 = 5 × 4 = 20 — correct.
More examples:
Enter a number and a shift count: 1 10
1 << 10 = 1024
210 = 1024 — the classic "compute a power of 2 by shifting 1."
Enter a number and a shift count: 255 1
255 << 1 = 510
255 × 2 = 510 — same result, from a very different bit pattern.
The variant with binary display:
Enter a number (0-255) and shift count (0-7): 5 2
n = 0000 0101
n << 2 = 0001 0100
Decimal result: 20
The binary view makes it clear: the bits of 5 (0000 0101) moved two positions left, filling zeros on the right, producing 0001 0100 = 20.
Edge case — overflow when shifting too far:
Enter a number and a shift count: 16777216 8
16777216 << 8 = 0
16777216 = 224. Shifting it left by 8 gives 232, which is exactly 1 beyond the maximum representable value of a 32-bit int — so the result wraps to 0 (all bits shifted off the left edge, zeros fill in on the right). This is a form of overflow, technically undefined behavior for signed integers, but on two's-complement systems it produces 0.
Step-by-Step Explanation
1. Declarations: int n, k; — n is the value to shift, k is the shift count. Both are integers. The shift operator works on any integer type.
2. Input validation: The check if (k < 0 || k >= 32) rejects shift counts outside the valid range. Shifting by a negative number or by a count ≥ the type's bit width is undefined behavior in C — the compiler may produce any result, including garbage or a crash. Always validate before shifting.
3. The shift operation: n << k moves each bit of n left by k positions. Each left shift multiplies the value by 2 (with wraparound if it overflows). 5 << 2 = 20 because 5 × 2² = 20.
4. Binary view: For 5 << 2:
5in binary:0000 0101- Shift left by 2:
0001 0100(each bit moves 2 positions left; the two rightmost bits become 0) 0001 0100= 20 in decimal
5. Signed vs unsigned: The behavior of << on signed integers with the sign bit set is undefined in the C standard (pre-C++20/C23 clarifications). On all modern compilers with two's complement, −1 << 1 gives −2 (arithmetic shift preserving sign), consistent with the multiply rule. For strict portability, use unsigned int for bit manipulation — the standard fully specifies shift behavior for unsigned types (no sign bit, no surprises).
6. Overflow: Shifting left past the type's width drops the high bits. 2,147,483,648 << 1 for int gives 0 (the only set bit (bit 31) is shifted off). This is undefined behavior for signed integers, but the multiply rule "wraps" predictably on two's-complement hardware.
7. Practical uses: 1 << k produces a mask with only bit k set — this is the standard way to test, set, or clear individual bits. n << 3 is a fast multiply by 8. Combining with OR builds bit patterns: (RED << 16) | (GREEN << 8) | BLUE packs three color bytes into one integer.
n << k shifts bits left, multiplying by 2k. Bits shifted off the left are lost; zeros fill in on the right. Always validate the shift count (0 ≤ k < bit-width) — negative or excessive shifts are undefined behavior. Use 1 << k to build bit masks.
Frequently Asked Questions
1. Why is n << k the same as n × 2k?
Because in binary, shifting a value left by 1 doubles it — every bit moves to a position worth twice as much. Shifting by 2 multiplies by 4, shifting by 3 multiplies by 8, and so on. This is exactly the same as multiplying by 2k. The identity holds as long as the result doesn't overflow the type. This is why left shift is often used as a fast multiply-by-power-of-2.
2. What happens if I shift by a count ≥ the bit width?
It's undefined behavior in C — anything can happen, including a garbage result, a crash, or a "helpful" compiler diagnostic. For int (32 bits), shifting by 32 or more is undefined. For long long (64 bits), shifting by 64 or more is undefined. Always validate 0 ≤ k < bit-width before shifting. The bit-width is sizeof(type) * CHAR_BIT, usually 32 for int and 64 for long long.
3. What about shifting negative numbers?
Shifting a negative signed integer left is undefined in the C standard (specifically, the sign bit and shift behavior on negative numbers are implementation-defined until C23 clarifies some cases). On virtually all modern compilers (two's complement), −5 << 1 = −10, following the multiply rule. For fully portable code, use unsigned int for bit manipulation — shifting unsigned integers is fully specified (it's modular arithmetic).
4. How do I create a bit mask?
Use 1 << k to get a mask with only bit k set. To check if a bit is set: if (n & (1 << k)). To set a bit: n |= (1 << k). To clear a bit: n &= ~(1 << k). To toggle a bit: n ^= (1 << k). The shift operator builds the mask; the bitwise operators apply it. To combine masks, OR them: (1 << 3) | (1 << 5).
5. Is n * 2 faster than n << 1?
In modern code, the compiler usually generates the same machine instruction (or better) for both. The multiplication is clearer, and the compiler applies the strength reduction optimization to convert n * 2 into a shift. Use n * 2 for readability; use n << 1 when the intent is explicitly bit-level manipulation (masking, flag setting). Never use << just to "save cycles" — the compiler knows what it's doing. Both forms are idiomatic in different contexts.
💡 Tip: once this works, try these classic left shift applications: (a) compute powers of 2 (1 << k), (b) build color values ((r << 16) | (g << 8) | b), (c) set bits (n |= (1 << k)), (d) fast multiply by powers of 2 (n << k), (e) construct hash multipliers (hash * 31 is often written (hash << 5) - hash in older C code). Each is a small extension of the same operator.