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 ShiftAmount positions
  • 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

ParameterDescription
NumberAny integer expression (can be positive or negative)
ShiftAmountNumber 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

  1. Extracting flag values from bit fields (e.g., permission integers)
  2. Dividing by powers of 2 efficiently (faster than / operator in some cases)
  3. Decoding packed integer data from APIs or legacy systems
  4. 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

ExpressionResultBinary (8-bit for clarity)
BITRSHIFT(32, 1)1600100000 → 00010000
BITRSHIFT(32, 3)400100000 → 00000100
BITRSHIFT(5, 1)200000101 → 00000010
BITRSHIFT(5, 2)100000101 → 00000001

Tip: Use BITRSHIFT(value, -n) for left shift (equivalent to BITLSHIFT(value, n)).

Comments