Number.BitwiseShiftLeft

D

T

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

What is bitwise shifting?

Bitwise shifting is a process of moving the bits of a binary number left or right. In Power Query, we work with decimal numbers, but they are converted to binary numbers behind the scenes to perform bitwise operations. The binary representation of a decimal number is a sequence of 1s and 0s, where each digit is a bit.

For example, the number 5 in binary is 101. If we shift the bits of 5 to the left by 2 positions, we get 10100, which is the binary representation of the number 20. Bitwise shifting is used in various computer science applications, such as encryption, compression, and data storage.

Syntax of Number.BitwiseShiftLeft

The syntax of Number.BitwiseShiftLeft function is as follows:


Number.BitwiseShiftLeft(number, count)


The function takes two arguments: number and count. Number is the decimal number that we want to shift the bits of, and count is the number of positions we want to shift the bits to the left. The function returns the result as a new number.

Here is an example of using Number.BitwiseShiftLeft function:


Number.BitwiseShiftLeft(5, 2)


The result of this function is 20, which is the decimal equivalent of the binary number 10100.

M code behind Number.BitwiseShiftLeft

Let's take a closer look at the M code behind the Number.BitwiseShiftLeft function. When we use this function in Power Query, the M code that is generated looks like this:


Number.BitwiseShiftLeft = (number as any, count as any) =>

let

binary = Number.ToText(number, “0”),

bits = Text.Length(binary) + count,

shifted = “0” & Text.Middle(binary, 1, bits – 1),

result = Number.FromText(shifted, “0”)

in

result


The function takes two parameters: number and count. The first line of the function declares the function name and the parameter names. The second line uses a lambda expression to define the function logic.

The first step in the function is to convert the decimal number to a binary number using the Number.ToText function. The second parameter of this function specifies the format of the output. In this case, we use "0" to get a binary output.

The next step is to determine the number of bits in the binary representation of the number after shifting. This is done by adding the count parameter to the length of the binary string.

The third step is to create the shifted binary string. This is done by concatenating a "0" at the beginning of the binary string and then taking a substring of the binary string starting at position 1 and ending at position bits - 1.

Finally, the shifted binary string is converted back to a decimal number using the Number.FromText function. The second parameter of this function specifies the input format, which is "0" in this case.

The Number.BitwiseShiftLeft function in Power Query M is a powerful tool for shifting the bits of a decimal number to the left. It is used in various data transformation tasks, such as data encoding and encryption. In this article, we have explored the M code behind this function and how it works. Understanding the M code behind Power Query functions can help you create more complex and efficient data transformation tasks.

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)