Number.BitwiseShiftRight

D

T

The M Code Behind the Power Query M function Number.BitwiseShiftRight

What is a Bitwise Right Shift?

Before we dive into the M code behind the Number.BitwiseShiftRight function, it is important to understand what a bitwise right shift is. A bitwise right shift is an operation that shifts the bits in a binary number to the right by a certain number of positions. The bits that are shifted out of the binary number are discarded, and the bits that are shifted in from the left are set to zero.

For example, let’s say we have the binary number 10101110. If we perform a bitwise right shift by 2 positions, the result would be 00101011. The two leftmost bits are shifted out of the binary number, and the two rightmost bits are set to zero.

The M Code Behind Number.BitwiseShiftRight

Now that we understand what a bitwise right shift is, let’s take a look at the M code behind the Number.BitwiseShiftRight function. The syntax for this function is as follows:


Number.BitwiseShiftRight(number as nullable any, count as number) as nullable any


The function takes two arguments: the number to be shifted and the number of positions to shift it to the right. The function returns the shifted number.

Here's an example of how to use the Number.BitwiseShiftRight function in Power Query M:


let

Source = #table({“Number”},{{10}}),

Shifted = Table.TransformColumns(Source, {“Number”, each Number.BitwiseShiftRight(_, 2), type number})

in

Shifted


In this example, we create a table with a single column called "Number" that contains the value 10. We then use the Table.TransformColumns function to apply the Number.BitwiseShiftRight function to the "Number" column, shifting it two positions to the right. The result is a new table with a single column called "Number" that contains the value 2.

Use Cases for Number.BitwiseShiftRight

The Number.BitwiseShiftRight function can be used in a variety of ways in your Power Query M code. Here are a few examples:

1. Converting Binary to Decimal

One common use case for the Number.BitwiseShiftRight function is converting a binary number to a decimal number. For example, let's say we have the binary number 10101110. We can convert this binary number to a decimal number by performing a bitwise right shift on each group of 4 bits and adding up the results. Here's an example M code that does this:


let

BinaryNumber = “10101110”,

Shifted1 = Number.BitwiseShiftRight(Number.FromText(BinaryNumber), 4),

Shifted2 = Number.BitwiseShiftRight(Number.FromText(BinaryNumber), 8),

Shifted3 = Number.BitwiseShiftRight(Number.FromText(BinaryNumber), 12),

Result = Shifted1 + Shifted2 + Shifted3

in

Result


In this example, we first convert the binary number to a decimal number using the Number.FromText function. We then perform three bitwise right shifts, each shifting the binary number 4 positions to the right. We add up the results of these shifts to get the decimal equivalent of the binary number.

2. Extracting Bits from a Number

Another use case for the Number.BitwiseShiftRight function is extracting individual bits from a number. For example, let's say we have the binary number 10101110. We can extract the second and third bits from this number by performing a bitwise right shift by 2 positions and then performing a bitwise and with the number 3 (which is represented in binary as 00000011). Here's an example M code that does this:


let

BinaryNumber = “10101110”,

Shifted = Number.BitwiseShiftRight(Number.FromText(BinaryNumber), 2),

Result = Bitwise.BitwiseAnd(Shifted, 3)

in

Result


In this example, we first convert the binary number to a decimal number using the Number.FromText function. We then perform a bitwise right shift by 2 positions to extract the second and third bits. We then perform a bitwise and with the number 3 to extract only those two bits.

In this article, we explored the M code behind the Power Query M function Number.BitwiseShiftRight. We learned what a bitwise right shift is, how to use the Number.BitwiseShiftRight function, and some of its various use cases. By understanding the M code behind this function, you can leverage its power to transform and manipulate your data in new and exciting ways.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)