Shifts
Left and right shift.
Shifts is a free C Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Shifting Bits
The shift operators move all bits of an integer left or right by a number of positions.
Left shift is << and right shift is >>.
#include <stdio.h>
int main(void) {
unsigned x = 1;
printf("1 << 3 = %u\n", x << 3);
return 0;
}Left Shift Multiplies
Shifting left by n positions multiplies an unsigned value by 2 to the power n.
1 << 3 is 8, and 5 << 1 is 10.
#include <stdio.h>
int main(void) {
printf("5 << 1 = %u\n", 5u << 1);
printf("3 << 4 = %u\n", 3u << 4);
return 0;
}Right Shift Divides
Shifting right by n divides an unsigned value by 2 to the power n, discarding the bits that fall off the right end.
20 >> 2 is 5.
#include <stdio.h>
int main(void) {
printf("20 >> 2 = %u\n", 20u >> 2);
printf("7 >> 1 = %u\n", 7u >> 1);
return 0;
}Powers of Two
Left shifting 1 is the fastest way to generate powers of two.
1 << n is the n-th power of two.
#include <stdio.h>
int main(void) {
for (int n = 0; n < 6; n++) {
printf("2^%d = %u\n", n, 1u << n);
}
return 0;
}Visualizing a Shift
Printing the bits before and after shows how the pattern slides.
#include <stdio.h>
void print_bits(unsigned char v) {
for (int i = 7; i >= 0; i--) putchar((v >> i) & 1 ? '1' : '0');
putchar('\n');
}
int main(void) {
unsigned char v = 0b00000110;
print_bits(v);
print_bits(v << 2);
return 0;
}Extracting a Bit
To read bit number i, shift the value right by i and mask with 1.
The result is 0 or 1.
#include <stdio.h>
int main(void) {
unsigned v = 0b10110;
for (int i = 0; i < 5; i++) {
printf("bit %d = %u\n", i, (v >> i) & 1);
}
return 0;
}Building a Value Bit by Bit
You can assemble a number by shifting bits into place and combining them with OR.
#include <stdio.h>
int main(void) {
unsigned v = 0;
v |= (1u << 0);
v |= (1u << 2);
v |= (1u << 4);
printf("v = %u\n", v);
return 0;
}Signed Right Shift Caution
Right shifting a negative signed integer is implementation-defined: it may copy the sign bit.
For predictable bit operations, prefer unsigned types.
#include <stdio.h>
int main(void) {
int s = -8;
printf("-8 >> 1 = %d (implementation-defined)\n", s >> 1);
unsigned u = 8;
printf("8u >> 1 = %u\n", u >> 1);
return 0;
}Avoid Over-Shifting
Shifting by an amount greater than or equal to the type width is undefined behavior.
Shifting a 32-bit value by 32 or more is not allowed.
#include <stdio.h>
#include <limits.h>
int main(void) {
printf("unsigned has %zu bits\n", sizeof(unsigned) * CHAR_BIT);
printf("Safe shift 1u << 31 = %u\n", 1u << 31);
return 0;
}Combining Shift and Mask
Shift and mask together extract a field of several bits. Here we pull the middle 4 bits out of a byte.
#include <stdio.h>
int main(void) {
unsigned v = 0b11010110;
unsigned field = (v >> 2) & 0b1111;
printf("field = %u\n", field);
return 0;
}Fast Multiply and Add
Combining shifts lets you multiply by constants quickly. Multiplying by 10 equals shifting by 3 (times 8) plus shifting by 1 (times 2).
#include <stdio.h>
int main(void) {
unsigned x = 7;
unsigned times10 = (x << 3) + (x << 1);
printf("7 * 10 = %u\n", times10);
return 0;
}Quick Check
Test your knowledge of shifts.
Recap
You learned about shift operators:
<<shifts left and multiplies by powers of two.>>shifts right and divides by powers of two.(v >> i) & 1extracts a single bit.- Use
unsignedfor predictable shifts and never shift by the type width or more.
Frequently asked questions
Is the “Shifts” lesson free?
Yes — the full text of “Shifts” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.
What will I learn in “Shifts”?
Left and right shift. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Shifts” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.