📖 Scenario: You are working with a simple device that uses bits to store information. You want to learn how to move bits left and right to change numbers quickly.
🎯 Goal: You will create a program that uses left shift and right shift operators to move bits in a number and see the results.
📋 What You'll Learn
Create an integer variable with a specific value
Create variables for the number of positions to shift
Use left shift operator to shift bits to the left
Use right shift operator to shift bits to the right
Print the results of the shifts
💡 Why This Matters
🌍 Real World
Bit shifting is used in low-level programming, such as controlling hardware, encoding data, and optimizing calculations.
💼 Career
Understanding bit shifts is important for embedded systems developers, game programmers, and anyone working close to the hardware.
Progress0 / 4 steps
1
Create the initial number
Create an integer variable called number and set it to 8.
C
Hint
Use int number = 8; to create the variable.
2
Create shift amount variables
Add two integer variables called left_shift and right_shift. Set left_shift to 2 and right_shift to 1.
C
Hint
Use int left_shift = 2; and int right_shift = 1;.
3
Apply left shift and right shift
Create two integer variables called shifted_left and shifted_right. Set shifted_left to number left shifted by left_shift. Set shifted_right to number right shifted by right_shift.
C
Hint
Use << for left shift and >> for right shift.
4
Print the results
Print the values of shifted_left and shifted_right using printf. Print shifted_left first, then shifted_right, each on its own line.
C
Hint
Use printf("%d\n", shifted_left); and printf("%d\n", shifted_right);.
Practice
(1/5)
1. What does the left shift operator << do to an integer in C?
easy
A. Moves bits to the left, multiplying the number by powers of two
B. Moves bits to the right, dividing the number by powers of two
C. Reverses the bits of the number
D. Sets all bits to zero
Solution
Step 1: Understand left shift operation
The left shift operator moves all bits in the number to the left by the specified count.
Step 2: Effect on the number's value
Each left shift multiplies the number by 2, so shifting left by n multiplies by 2^n.
Final Answer:
Moves bits to the left, multiplying the number by powers of two -> Option A
Quick Check:
Left shift = multiply by 2^n [OK]
Hint: Left shift multiplies by 2 to the power of shift count [OK]
Common Mistakes:
Confusing left shift with right shift
Thinking left shift divides the number
Assuming left shift reverses bits
2. Which of the following is the correct syntax to shift the integer variable x right by 3 bits in C?
easy
A. x >>> 3;
B. x >> 3;
C. x << 3;
D. x >>3
Solution
Step 1: Identify the right shift operator in C
The right shift operator is >> and requires spaces or no spaces around it.
Step 2: Check syntax correctness
x >> 3; is correct syntax. x << 3; is left shift, x >>> 3; is invalid in C, and x >>3 is missing a semicolon.
Final Answer:
x >> 3; -> Option B
Quick Check:
Right shift syntax = x >> n; [OK]
Hint: Right shift uses >> with semicolon to end statement [OK]
Common Mistakes:
Using >>> which is not valid in C
Confusing left shift << with right shift >>
Omitting semicolon at end
3. What is the output of the following C code?
int x = 4;
int y = x << 2;
printf("%d", y);
medium
A. 0
B. 1
C. 8
D. 16
Solution
Step 1: Understand the initial value of x
x is 4, which in binary is 00000100.
Step 2: Apply left shift by 2 bits
Shifting left by 2 bits multiplies 4 by 2^2 = 4 * 4 = 16.
Final Answer:
16 -> Option D
Quick Check:
4 << 2 = 16 [OK]
Hint: Left shift by n multiplies by 2^n [OK]
Common Mistakes:
Calculating 4 << 2 as 8 instead of 16
Confusing left shift with right shift
Ignoring the shift count
4. Find the error in this C code snippet:
int a = 32;
int b = a >> 2
printf("%d", b);
medium
A. Wrong operator used for right shift
B. Incorrect printf format specifier
C. Missing semicolon after right shift operation
D. Variable b is not declared
Solution
Step 1: Check syntax line by line
The line int b = a >> 2 is missing a semicolon at the end.
Step 2: Verify other parts
The operator >> is correct, printf uses %d for int, and b is declared properly.
Final Answer:
Missing semicolon after right shift operation -> Option C
Quick Check:
Every statement needs semicolon [OK]
Hint: Check for missing semicolons after shift operations [OK]
Common Mistakes:
Assuming >> is invalid operator
Thinking printf format is wrong
Ignoring missing semicolon errors
5. Given int x = 1;, what is the value of y after this operation?
int y = (x << 3) >> 1;
hard
A. 4
B. 8
C. 16
D. 2
Solution
Step 1: Calculate left shift (x << 3)
Left shifting 1 by 3 bits multiplies 1 by 2^3 = 8.
Step 2: Calculate right shift of result by 1
Right shifting 8 by 1 bit divides 8 by 2^1 = 2, so 8 / 2 = 4.
Final Answer:
4 -> Option A
Quick Check:
(1 << 3) >> 1 = 8 >> 1 = 4 [OK]
Hint: Left shift then right shift: multiply then divide by powers of two [OK]
Common Mistakes:
Calculating (1 << 3) as 16
Ignoring right shift effect
Mixing up shift directions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.