BinaryFormat.7BitEncodedSignedInteger

D

T

The M Code Behind the Power Query M function BinaryFormat.7BitEncodedSignedInteger

Let’s take a closer look at the M code behind this function and how it works.

The BinaryFormat.7BitEncodedSignedInteger function

The BinaryFormat.7BitEncodedSignedInteger function takes an integer as its input and returns a binary value that represents the compressed 7-bit format of that integer.

Here is an example of how to use this function:


BinaryFormat.7BitEncodedSignedInteger(100)


This would return the binary value `11000100`.

Understanding the M code

The M code behind the BinaryFormat.7BitEncodedSignedInteger function uses a combination of bitwise operators and conditional statements to compress the integer into a 7-bit format.

Here is the M code for the BinaryFormat.7BitEncodedSignedInteger function:


(n as number) as binary =>

let

result = if n >= 0 then

if n < 128 then n

else if n < 16384 then

(n % 128) + 128 + (n / 128) 256

else if n < 2097152 then

(n % 128) + 128 + ((n / 128) % 128) 256 + (n / 16384) 65536

else if n < 268435456 then

(n % 128) + 128 + ((n / 128) % 128) 256 + ((n / 16384) % 128) 65536 + (n / 2097152) 16777216

else null

else

if n >= -64 then 64 + n

else if n >= -8192 then

(n / 128) – 64 + 128

else if n >= -1048576 then

(n / 65536) – 64 + 128 + ((n / 128) % 128) 256

else if n >= -134217728 then

(n / 16777216) – 64 + 128 + ((n / 65536) % 128) 256 + ((n / 128) % 128) 65536

else null

in

if result <> null then Binary.FromText(Text.PadStart(Text.From(result), 8, “0”), BinaryEncoding.Base64)

else null


Let's break down this code into its individual components:

Bitwise operators

The BinaryFormat.7BitEncodedSignedInteger function uses a number of bitwise operators to manipulate the bits of the integer being compressed. These operators include:

- `%` (modulus): Returns the remainder of dividing one number by another.

- `/` (division): Divides one number by another.

- `&` (bitwise AND): Returns a value where each bit is set to 1 only if both corresponding bits in the input values are 1.

- `|` (bitwise OR): Returns a value where each bit is set to 1 if either corresponding bit in the input values is 1.

- `<<` (bitwise left shift): Shifts the bits of a number to the left by a specified number of places.

- `>>` (bitwise right shift): Shifts the bits of a number to the right by a specified number of places.

Conditional statements

The BinaryFormat.7BitEncodedSignedInteger function also uses a number of conditional statements to determine how to compress the integer. These statements include:

- `if...then...else`: Evaluates a condition and returns one of two values depending on whether the condition is true or false.

- `null`: Represents a null value.

The BinaryFormat.7BitEncodedSignedInteger function is a powerful tool for compressing integers in Power Query. By understanding the M code behind this function, you can gain a deeper understanding of how it works and how to use it effectively in your own projects.

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)