C Right Shift Assignment Operator
In C, the Right Shift Assignment >>=
operator is used to shift the bits of a variable to the right by a specified number of positions and assign the result back to the variable. This operation effectively divides the value by 2^n
, where n
is the number of positions shifted.
Syntax of the Right Shift Assignment Operator
The syntax for the Right Shift Assignment operator is:
variable >>= n;
Explanation:
variable
: The integer variable whose bits will be shifted.>>=
: The Right Shift Assignment operator.n
: The number of positions to shift the bits to the right.- Return Value: The value of
variable
after shifting its bitsn
positions to the right.
Examples of the Right Shift Assignment Operator
1. Using Right Shift Assignment on a Positive Integer
In this example, we will use the Right Shift Assignment operator >>=
to shift the bits of a positive integer and print the result.
main.c
#include <stdio.h>
int main() {
int num = 32;
num >>= 2; // Shift bits right by 2 positions
printf("Result after right shift: %d\n", num);
return 0;
}
Explanation:
- We declare an integer variable
num
and initialize it with32
(binary:00100000
). - The statement
num >>= 2
shifts the bits ofnum
two places to the right. - This is equivalent to
32 / (2^2) = 32 / 4 = 8
. - The result is printed using
printf()
.
Output:
Result after right shift: 8
2. Using Right Shift Assignment on a Negative Integer
In this example, we will use the Right Shift Assignment operator >>=
on a negative number to observe its behavior.
main.c
#include <stdio.h>
int main() {
int num = -32;
num >>= 2; // Shift bits right by 2 positions
printf("Result after right shift: %d\n", num);
return 0;
}
Explanation:
- We initialize
num
to-32
(binary representation depends on the system’s signed integer representation). - The statement
num >>= 2
shifts the bits ofnum
two positions to the right. - For negative numbers, the behavior depends on whether the system uses arithmetic shift (preserving the sign bit) or logical shift.
- On most systems, arithmetic shift is used, so
-32 / 4
results in-8
. - The result is printed using
printf()
.
Output:
Result after right shift: -8
3. Using Right Shift Assignment in a Loop
In this example, we will demonstrate the use of the Right Shift Assignment operator inside a while loop to repeatedly divide a number by 2.
main.c
#include <stdio.h>
int main() {
int num = 64;
while (num > 0) {
printf("%d\n", num);
num >>= 1; // Right shift by 1 position
}
return 0;
}
Explanation:
- We initialize
num
with64
. - The
while
loop runs as long asnum > 0
. - Inside the loop, we print the current value of
num
. - The statement
num >>= 1
shifts the bits right by 1, halving the value. - The process continues until
num
reaches 0.
Output:
64
32
16
8
4
2
1
Conclusion
In this tutorial, we covered the Right Shift Assignment >>=
operator in C:
- The operator shifts bits to the right and assigns the result back to the variable.
- It effectively divides the value by
2^n
, wheren
is the shift count. - For negative numbers, it behaves differently depending on the system (arithmetic vs. logical shift).
- It can be used in loops for efficient halving operations.