Explore how to use the left shift operator (<<
) in C with this comprehensive guide. Learn how the operator shifts the bits of a number to the left, effectively multiplying the number by powers of two. This tutorial includes clear explanations, practical examples, and code snippets to help you understand the application of the left shift operator in various programming scenarios.
#include <stdio.h>
int main() {
int num = 5;
printf("Original number: %d\n", num);
num = num << 2; // Left shifting the number by 2 bits
printf("After left shift by 2: %d\n", num);
return 0;
}
Inside the main() function:
- An integer variable num is initialized with the value 5.
- The original value of num is printed to the console.
- The num variable is left shifted by 2 bits using the `<<` operator.
- The result after left shift is printed to the console.