Right Shift Operation:
From: | To: |
The right shift operator (>>) shifts the bits of a number to the right by a specified number of positions. It's equivalent to integer division by 2 raised to the power of the shift amount.
The right shift operation can be represented as:
Which is equivalent to:
Where:
Example: 16 >> 2 = 4 (since 16/2² = 4)
Details: Right shift operations are commonly used in:
Tips: Enter any integer number and the number of bits to shift right. The result will be an integer value.
Q1: What happens when you right shift a negative number?
A: The behavior depends on the programming language. In most cases, it performs an arithmetic right shift (preserves the sign bit).
Q2: Is right shift the same as division by 2?
A: Only for positive numbers. For negative numbers, results may differ between right shift and division.
Q3: What's the difference between >> and >>> operators?
A: >> is arithmetic right shift (preserves sign), >>> is logical right shift (doesn't preserve sign, fills with zeros).
Q4: What happens if I shift more bits than the size of the number?
A: The result is implementation-dependent but typically becomes 0 for positive numbers and -1 for negative numbers.
Q5: Why use right shift instead of division?
A: Right shift is often faster than division at the hardware level, making it useful for performance-critical code.