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:

</>
Copy
result = number >> n;

Explanation:

  1. number: The integer whose bits are to be shifted.
  2. >>: The bitwise right shift operator.
  3. n: The number of positions to shift the bits to the right.
  4. result: The value obtained after shifting. It is equal to number / 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

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

  1. We declare an integer variable number with a value of 16.
  2. We apply the right shift operator >> 2, shifting the bits 2 positions to the right.
  3. Mathematically, 16 / 2^2 = 16 / 4 = 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

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

  1. We declare an integer variable number with a value of -32.
  2. We apply the right shift operator >> 2, shifting the bits 2 positions to the right.
  3. 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.
  4. 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

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

  1. We initialize number to 64.
  2. Inside a while loop, we print the current value of number.
  3. We use number = number >> 1 to shift right by 1 in each iteration.
  4. 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:

  1. The Right Shift >> operator moves bits to the right by n positions.
  2. It effectively divides the number by 2^n.
  3. It behaves differently for positive and negative numbers.
  4. It is useful for bitwise operations and optimizing calculations.