Number.BitwiseShiftRight
NumberShifts the bits of an integer right by a specified number of positions.
Syntax
Number.BitwiseShiftRight(number as number, shift as number) as numberParameters
| Name | Type | Required | Description |
|---|---|---|---|
number | number | Yes | The integer whose bits are shifted. |
shift | number | Yes | The number of bit positions to shift right. Must be a non-negative integer. |
Return Value
number — The integer with bits shifted right by the specified number of positions.
Remarks
Number.BitwiseShiftRight shifts the binary representation of number to the right by shift positions. It performs an arithmetic right shift: the high-order bits are filled with the sign bit of the original value (0 for positive numbers, 1 for negative numbers). This means shifting a non-negative integer right by n is equivalent to integer division by 2^n (truncating toward zero).
This is the complement of Number.BitwiseShiftLeft. Common uses include:
- Extracting packed bit fields: shift a packed integer right to bring the desired field to the low-order bits, then AND with a mask
- Efficient powers-of-2 division: Number.BitwiseShiftRight(x, n) is a fast alternative to Number.IntegerDivide(x, Number.Power(2, n))
- Undoing a left shift: recover the original value that was shifted left
Note that for negative numbers, the sign-extension fill means right-shifting produces increasingly negative values (not unsigned logical shift). Power Query M uses arithmetic (signed) right shift for all Number.BitwiseShiftRight operations.
Examples
Example 1: Shift 16 right by 2 positions — equivalent to 16 ÷ 4
Number.BitwiseShiftRight(16, 2)Result | |
|---|---|
| 1 | 4 |
Example 2: Extract the upper nibble (bits 4–7) from a byte value
Number.BitwiseShiftRight(181, 4)Result | |
|---|---|
| 1 | 11 |
Example 3: Round-trip through shift left and shift right
let
Original = 5,
Shifted = Number.BitwiseShiftLeft(Original, 3),
Restored = Number.BitwiseShiftRight(Shifted, 3)
in
RestoredThe final output — the round-trip result, confirming the original value 5 is fully restored.
Result | |
|---|---|
| 1 | 5 |