In my recent quest to create or catalog as many DAX equivalents for Excel functions, with DEC2BIN and BIN2DEC figured out, we can implement bit shifting functions such as BITRSHIFT (Bit Right Shift):
BITLSHIFT =
VAR __DecimalNumber = SELECTEDVALUE('Decimal'[Decimal],0)
VAR __Bits = SELECTEDVALUE('Bits'[Bits],0)
VAR __BitShift = REPT("0",ABS(__Bits))
VAR __Binary =
IF(
__DecimalNumber = 0,
"0",
VAR __Table =
ADDCOLUMNS(
GENERATESERIES(0,LOG(__DecimalNumber,2),1),
"Bit",MOD(TRUNC(__DecimalNumber / POWER(2,[Value])),2)
)
RETURN
CONCATENATEX(__Table,[Bit],"",[Value],DESC)
)
VAR __Shifted =
IF(
__Bits >= 0,
LEFT(__Binary,LEN(__Binary) - __Bits),
__Binary & __BitShift,
)
VAR __Table =
ADDCOLUMNS(
GENERATESERIES(0,LEN(__Shifted)-1,1),
"Decimal",POWER(2,[Value]) * MID(__Shifted,LEN(__Shifted) - [Value],1)
)
RETURN
SUMX(__Table,[Decimal])
NOTE: Does it support negative numbers you ask? Yes and no. It does not support negative input decimal numbers to shift but then neither does the Excel BITRSHIFT function. However, you can have a negative number of bits to shift just like the Excel version. Using a negative number of bits effectively makes this a BITLSHIFT.
eyJrIjoiYzkwMzQxYjAtNjZhMC00OGJmLWFjNjUtYWQxNjlmNjM5MmUxIiwidCI6IjRhMDQyNzQzLTM3M2EtNDNkMi04MjdiLTAwM2Y0YzdiYTFlNSIsImMiOjN9