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:

</>
Copy
variable >>= n;

Explanation:

  1. variable: The integer variable whose bits will be shifted.
  2. >>=: The Right Shift Assignment operator.
  3. n: The number of positions to shift the bits to the right.
  4. Return Value: The value of variable after shifting its bits n 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

</>
Copy
#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:

  1. We declare an integer variable num and initialize it with 32 (binary: 00100000).
  2. The statement num >>= 2 shifts the bits of num two places to the right.
  3. This is equivalent to 32 / (2^2) = 32 / 4 = 8.
  4. 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

</>
Copy
#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:

  1. We initialize num to -32 (binary representation depends on the system’s signed integer representation).
  2. The statement num >>= 2 shifts the bits of num two positions to the right.
  3. For negative numbers, the behavior depends on whether the system uses arithmetic shift (preserving the sign bit) or logical shift.
  4. On most systems, arithmetic shift is used, so -32 / 4 results in -8.
  5. 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

</>
Copy
#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:

  1. We initialize num with 64.
  2. The while loop runs as long as num > 0.
  3. Inside the loop, we print the current value of num.
  4. The statement num >>= 1 shifts the bits right by 1, halving the value.
  5. 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:

  1. The operator shifts bits to the right and assigns the result back to the variable.
  2. It effectively divides the value by 2^n, where n is the shift count.
  3. For negative numbers, it behaves differently depending on the system (arithmetic vs. logical shift).
  4. It can be used in loops for efficient halving operations.