Daily DAX : Day 409 BITRSHIFT
Power BI DAX – BITRSHIFT Function
Syntax:
BITRSHIFT(Number, ShiftAmount)
What It Does
Performs a logical right shift on an integer value.
- Takes the binary representation of
Number - Shifts all bits to the right by
ShiftAmountpositions - Fills the leftmost bits with zeros (this is what makes it a logical shift)
- Returns the resulting integer
Note: Unlike arithmetic right shift (which preserves the sign bit),
BITRSHIFT always fills with zeros, so it works the same for positive and negative numbers in two's complement representation (DAX treats negative numbers as large positive equivalents in bit operations).
Parameters
| Parameter | Description |
|---|---|
| Number | Any integer expression (can be positive or negative) |
| ShiftAmount | Number of positions to shift right. Positive = right shift, Negative = left shift (same as BITLSHIFT) |
Examples
BITRSHIFT(8, 1) = 4 // 1000 → 0100
BITRSHIFT(8, 2) = 2 // 1000 → 0010
BITRSHIFT(10, 1) = 5 // 1010 → 0101
BITRSHIFT(10, 3) = 1 // 1010 → 0001
BITRSHIFT(-8, 1) = 2147483644 // Large positive number due to two's complement
Common Use Cases
- Extracting flag values from bit fields (e.g., permission integers)
- Dividing by powers of 2 efficiently (faster than / operator in some cases)
- Decoding packed integer data from APIs or legacy systems
- Working with RGB colors or other bit-packed structures
Practical Example: Extract Red component from RGB integer
Suppose a column RGBValue contains packed RGB as a single integer (e.g., 4283411563 = #FF55008B)
// Extract Red (most significant 8 bits)
Red = BITAND(BITRSHIFT([RGBValue], 16), 255)
// Extract Green
Green = BITAND(BITRSHIFT([RGBValue], 8), 255)
// Extract Blue
Blue = BITAND([RGBValue], 255)
Quick Reference Table
| Expression | Result | Binary (8-bit for clarity) |
|---|---|---|
| BITRSHIFT(32, 1) | 16 | 00100000 → 00010000 |
| BITRSHIFT(32, 3) | 4 | 00100000 → 00000100 |
| BITRSHIFT(5, 1) | 2 | 00000101 → 00000010 |
| BITRSHIFT(5, 2) | 1 | 00000101 → 00000001 |
Tip: Use BITRSHIFT(value, -n) for left shift (equivalent to BITLSHIFT(value, n)).
Comments
Post a Comment