Table of Contents
The right shift operator >> shifts the bits of an integer to the right by the specified number of positions. Bits shifted beyond the right side are discarded.
#include <stdio.h>
int main()
{
unsigned int number = 20;
printf("number >> 1 = %u\n", number >> 1);
printf("number >> 2 = %u\n", number >> 2);
return 0;
}
Example Output #
number >> 1 = 10
number >> 2 = 5
Explanation #
The value 20 in binary is:
00010100
Shifting it right by one position:
00010100 >> 1
00001010
The resulting value is 10.
Shifting it right by two positions:
00010100 >> 2
00000101
The resulting value is 5.
For an unsigned integer, right shifting by n positions corresponds to integer division by 2^n, with the fractional part discarded.
For signed integers, the result of a right shift of a negative value is implementation-defined, so unsigned integers are used here to make the behavior unambiguous.