To multiply a number by 4 using bitwise operators in C, shift it left by two bits: number << 2. Each left shift by one position doubles the value, exactly the way appending a zero multiplies a decimal number by ten — binary is base 2, so shifting one place multiplies by 2, two places by 4, three by 8. This is a favorite interview warm-up because it tests whether you understand what the bits actually do, and it comes with two traps worth knowing: shifting a negative number left is undefined behavior in C, and shifting bits off the top of the type overflows. This page shows the tested, warning-free C89 program with both guards, plus what the shift looks like bit by bit.
How It Works — Step by Step
- Place value in binary: the bit positions are worth 1, 2, 4, 8, 16… Moving every bit one position left doubles each bit’s worth — so the whole value doubles.
- Take 25 (
11001): shifting left two places gives1100100, which is 100. The bit pattern is unchanged — it just moved. - Combine shifts for other factors:
x << 3is ×8;(x << 2) + xis ×5;(x << 3) - xis ×7.
| Expression | Meaning | 25 becomes |
|---|---|---|
x << 1 |
×2 | 50 |
x << 2 |
×4 | 100 |
x << 3 |
×8 | 200 |
x >> 1 |
÷2 (integer) | 12 |
C Program to Multiply a Number by 4 Using Left Shift
/* Multiply a number by 4 using the left shift operator
* Compile: gcc -ansi -Wall -Wextra multiply_by_4.c -o multiply_by_4 */
#include <stdio.h>
#include <limits.h>
int main(void)
{
long number;
printf("Enter a non-negative integer: ");
if (scanf("%ld", &number) != 1) {
fprintf(stderr, "Invalid input.\n");
return 1;
}
if (number < 0) {
fprintf(stderr, "Left-shifting a negative number is undefined "
"behavior in C.\n");
return 1;
}
if (number > LONG_MAX / 8) {
fprintf(stderr, "Number too large: shifting would overflow "
"a long.\n");
return 1;
}
printf("%ld x 2 = %ld (number << 1)\n", number, number << 1);
printf("%ld x 4 = %ld (number << 2)\n", number, number << 2);
printf("%ld x 8 = %ld (number << 3)\n", number, number << 3);
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra multiply_by_4.c -o multiply_by_4
./multiply_by_4
Sample Input and Output
Test 1:
Enter a non-negative integer: 25 25 x 2 = 50 (number << 1) 25 x 4 = 100 (number << 2) 25 x 8 = 200 (number << 3)
Test 2:
Enter a non-negative integer: 7 7 x 2 = 14 (number << 1) 7 x 4 = 28 (number << 2) 7 x 8 = 56 (number << 3)
Test 3 — the undefined-behavior guard:
Enter a non-negative integer: -3 Left-shifting a negative number is undefined behavior in C.
Test 4 — the overflow guard:
Enter a non-negative integer: 2000000000000000000 Number too large: shifting would overflow a long.
All outputs are real captured runs of the exact code above.
Code Explanation
- Why negatives are rejected: the C standard says left-shifting a negative value is undefined behavior (C99 §6.5.7) — the sign bit’s involvement makes the result unspecifiable across machines. The guard isn’t pedantry; it’s the difference between a correct program and one that happens to work on your compiler today.
- The overflow guard: shifting bits past the top of a
longis also undefined for signed types, so the program rejects anything aboveLONG_MAX / 8before shifting (8 covers our largest shift,<< 3).LONG_MAXcomes from<limits.h>. - Does this beat
number * 4? Not anymore. Every modern compiler emits the identical shift instruction for* 4at any optimization level. Write* 4for clarity in real code — and understand<< 2because you’ll meet it in embedded code, hash functions, and interviews. - Right shift is integer division:
25 >> 1is 12, not 12.5 — the shifted-out bit is simply dropped, exactly like25 / 2in integer arithmetic.
What This Program Teaches
- Binary place value — why moving bits multiplies and divides by powers of 2
- Undefined behavior awareness — negative shifts and overflow are UB, not “weird results”
- Compiler literacy — optimizers already turn
* 4into a shift; write for humans - Input validation — checking
scanf()‘s return value before trusting the variable
Related C Programs
- Bit Flipping in C — setting, clearing, and toggling individual bits
- 2’s Complement in C — how negative numbers are actually stored
- Increment and Decrement Operators in C — more operator subtleties
Test yourself: our free C Programming Quiz app for Android has 150+ questions with explanations for every answer — the Operators category covers shifts in depth.
Recommended Book
Section 2.9 of The C Programming Language by Kernighan & Ritchie covers the bitwise operators used here. We’ve solved all of the book’s exercises. Also on Amazon.com.