Learn how to use the right shift operator (>>
) in C with this detailed guide. This tutorial demonstrates how the operator shifts the bits of a number to the right, effectively dividing the number by powers of two. It includes clear explanations, practical examples, and code snippets to help you grasp the concept and apply the right shift operator effectively in your C programs.
#include <stdio.h>
int main() {
int num = 20;
printf("Original number: %d\n", num);
num = num >> 2; // Right shifting the number by 2 bits
printf("After right shift by 2: %d\n", num);
return 0;
}
Inside the main() function:
- An integer variable num is initialized with the value 20.
- The original value of num is printed to the console.
- The num variable is right shifted by 2 bits using the `>>` operator.
- The result after right shift is printed to the console.