The left shift operator << slides a number’s bits toward the high end — and every position shifted doubles the value. This question from our C Programming Quiz App checks whether you read 1 << 3 as “shift 1 left by 3 places” or fall for one of the plausible misreadings.
The Quiz Question
printf("%d", 1 << 3);
What is printed by this code?
- 3
- 6
- 8
- 16
The Correct Answer: 8
Start with 1 (binary 0001) and shift its bit three places left: 1000, which is 8. Each shift multiplies by 2, so shifting by 3 multiplies by 2³. Verified on gcc 13.3 and Apple clang 21, warning-free:
$ gcc -Wall -Wextra shift.c && ./a.out 8
1 << 0 = 0001 = 1 1 << 1 = 0010 = 2 1 << 3 = 1000 = 8 1 << 4 = 10000 = 16
(All four lines above are from an actual run.) The general rule: x << n equals x * 2ⁿ as long as nothing overflows — we also verified 5 << 3 = 40, i.e. 5 × 8.
Why Each Wrong Answer Is Wrong
Why not 3?
3 is just the right-hand operand echoed back — a guess that << somehow “selects” the shift amount. The left operand is the value being shifted; the right operand only says how far.
Why not 6?
6 would be 3 << 1 or 1 + 2 + 3-style arithmetic — usually the result of reading << as multiplication by the operand (1 × 3 × 2?). Shifting multiplies by powers of two, not by the shift count: by 3 places means × 8, not × 3 or × 6.
Why not 16?
16 is 1 << 4 — the off-by-one answer. Shift counts start at 0: 1 << 0 is still 1. Three shifts from 1 land on 8, the fourth reaches 16.
Where << Shows Up in Real Code
1 << k is the standard way to build a bit mask for bit k — the pattern behind flags and options everywhere:
#define FLAG_READ (1 << 0) /* 0001 */
#define FLAG_WRITE (1 << 1) /* 0010 */
#define FLAG_EXEC (1 << 2) /* 0100 */
flags |= FLAG_WRITE; /* set a bit */
if (flags & FLAG_READ) ... /* test a bit */
Combined with OR to set and AND to test, shifting is how compact option sets are packed into a single integer.
The boundary that bites: shifting a 1 into or past the sign bit of a signed int is undefined behavior. 1 << 31 on a 32-bit int compiles silently on both gcc and clang and only UBSan calls it out — we dissected that exact trap (quiz #154) in our signed-overflow post. Shift counts ≥ the type’s width (like 1 << 32) or negative counts are undefined too. For bit work at the top of the range, use 1u or unsigned types.
Frequently Asked Questions
Is left shift the same as multiplying by 2?
Each position shifted doubles the value: x << n equals x × 2ⁿ, provided the result fits the type. 1 << 3 is 1 × 8 = 8, and 5 << 3 is 40.
When is left shift undefined behavior in C?
Three cases: shifting into/past the sign bit of a signed type (1 << 31 on 32-bit int), a shift count greater than or equal to the type’s width (1 << 32), and a negative shift count. Use unsigned operands (1u << 31) for high-bit masks.
Why does 1 << 3 give 8 and not 16?
Because counting starts at zero shifts: 1 << 0 = 1, << 1 = 2, << 2 = 4, << 3 = 8. Sixteen needs a fourth shift.
Related Reading
- 1 << 31 – Signed Overflow UB in C
- Right Shift Operator in C – Why 6 >> 1 Is 3
- Multiply by 4 Using Bitwise Operators
- C Aptitude Questions and Answers
Recommended Books
- The C Programming Language – Kernighan & Ritchie (India) | Amazon.com
- C Programming: A Modern Approach – K.N. King (India) | Amazon.com
This question is #11 in the C Programming Quiz App — 155 questions with explanations covering operators, pointers, memory, and more.
Download on Google Play →