C Bitwise Right Shift Operator
In C, the Bitwise Right Shift >>
operator shifts the bits of a number to the right by a specified number of positions. This operation effectively divides the number by 2^n
, where n
is the number of positions shifted. The rightmost bits are discarded, and new bits are added on the left based on the type of shift (logical or arithmetic).
The Right Shift >>
operator is commonly used in low-level programming and performance optimization.
Syntax of the Bitwise Right Shift Operator
The syntax to use the Bitwise Right Shift operator is:
result = number >> n;
Explanation:
number
: The integer whose bits are to be shifted.>>
: The bitwise right shift operator.n
: The number of positions to shift the bits to the right.result
: The value obtained after shifting. It is equal tonumber / 2^n
.
Examples of the Bitwise Right Shift Operator
1. Basic Right Shift Operation
In this example, we will shift the bits of a number two positions to the right and observe the result.
main.c
#include <stdio.h>
int main() {
int number = 16;
int result = number >> 2; // Right shift by 2 positions
printf("Original number: %d\n", number);
printf("After right shift by 2: %d\n", result);
return 0;
}
Explanation:
- We declare an integer variable
number
with a value of 16. - We apply the right shift operator
>> 2
, shifting the bits 2 positions to the right. - Mathematically,
16 / 2^2 = 16 / 4 = 4
. - The new value after shifting is stored in
result
and printed.
Output:
Original number: 16
After right shift by 2: 4
2. Right Shift with Negative Numbers
In this example, we will shift the bits of a negative number to the right and analyze how it behaves.
main.c
#include <stdio.h>
int main() {
int number = -32;
int result = number >> 2; // Right shift by 2 positions
printf("Original number: %d\n", number);
printf("After right shift by 2: %d\n", result);
return 0;
}
Explanation:
- We declare an integer variable
number
with a value of -32. - We apply the right shift operator
>> 2
, shifting the bits 2 positions to the right. - The behavior of right shift with negative numbers depends on the system:
- Some systems use arithmetic shift (sign bit remains), keeping the number negative.
- Other systems may use logical shift, filling new bits with 0.
- The result varies depending on whether arithmetic or logical shift is used.
Possible Output:
Original number: -32
After right shift by 2: -8
3. Right Shift in a Loop
In this example, we will use a loop to continuously right shift a number until it becomes zero.
main.c
#include <stdio.h>
int main() {
int number = 64;
while (number > 0) {
printf("%d\n", number);
number = number >> 1; // Right shift by 1
}
return 0;
}
Explanation:
- We initialize
number
to 64. - Inside a
while
loop, we print the current value ofnumber
. - We use
number = number >> 1
to shift right by 1 in each iteration. - The loop continues until
number
becomes 0.
Output:
64
32
16
8
4
2
1
Conclusion
In this tutorial, we covered the Bitwise Right Shift >>
operator in C. Important points to remember are:
- The Right Shift
>>
operator moves bits to the right byn
positions. - It effectively divides the number by
2^n
. - It behaves differently for positive and negative numbers.
- It is useful for bitwise operations and optimizing calculations.