0
0
Cprogramming~15 mins

Left shift and right shift in C - Mini Project: Build & Apply

Choose your learning style9 modes available
Left shift and right shift
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use printf("%d\n", shifted_left); and printf("%d\n", shifted_right);.