Bit r shift
BITRSHIFT Function¶
The BITRSHIFT function in Excel performs a bitwise right shift operation on a given number by shifting its binary
representation to the right by a specified number of bits. For each shift, the least significant bit (rightmost bit) is
removed, and a 0 is added to the most significant bit (leftmost position). This operation effectively divides the
number by 2^n, where n is the number of places to shift, discarding any remainder.
Key Features of BITRSHIFT:¶
- Shifts the binary representation of a number to the right by a specified number of bits.
- The result is equivalent to integer division by
2^n. - Useful for low-level programming, binary arithmetic, and data manipulation tasks.
Syntax:¶
- number: The non-negative integer to be shifted.
- shift_amount: The number of bit positions to shift:
- Must be an integer.
- A positive
shift_amountshifts bits to the right. - A negative
shift_amountshifts bits to the left (essentially behaving likeBITLSHIFT).
Examples:¶
-
Basic Right Shift:
=BITRSHIFT(8, 1)
Binary representation of8is1000. Shifting right by 1 bit gives0100(4 in decimal).
Result:4 -
Right Shift by Two Bits:
=BITRSHIFT(16, 2)
Binary representation of16is10000. Shifting right by 2 bits gives00100(4 in decimal).
Result:4 -
Shift Beyond Number of Bits:
=BITRSHIFT(5, 4)
Binary representation of5is101. Shifting right by 4 bits removes all bits, leaving only0.
Result:0 -
Negative Shift Amount:
=BITRSHIFT(3, -2)
A negative shift amount results in a left shift of 2 bits. Binary11becomes1100(12 in decimal).
Result:12 -
Error with Negative Number:
=BITRSHIFT(-3, 2)
Negative numbers are not valid for thenumberargument, so Excel returns a#NUM!error.
Result:#NUM!
Notes:¶
-
Non-Negative Numbers Only:
- The
numberargument must be a non-negative integer. A negative input results in a#NUM!error.
- The
-
Shifting by Zero:
- If
shift_amountis0, the result is the same as the inputnumber.
- If
-
Right Shift Behavior:
- For each bit shifted right, the number is divided by
2. - Bits that are shifted out of the rightmost position are discarded; zeros fill in from the left.
- For each bit shifted right, the number is divided by
-
Error Handling:
- If
numberis not an integer or is negative, Excel returns a#NUM!error. - If
shift_amountis not an integer, Excel returns a#VALUE!error.
- If
Applications:¶
-
Use Case:
- The
BITRSHIFTfunction is useful for performing quick integer division by powers of 2. - Commonly used in bit-level data manipulation, flag encoding/decoding, and binary protocols.
- The
-
Complementary Functions:
- BITLSHIFT: Shifts bits to the left by a specified number of places.
- BITAND: Performs a bitwise AND operation.
- BITOR: Performs a bitwise OR operation.
- BITXOR: Performs a bitwise XOR operation.