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

Understanding the Number.BitwiseXor Function

The Number.BitwiseXor function is used to perform a bitwise XOR operation between two numbers. It returns a number that represents the result of the operation. XOR stands for “exclusive or” and it is a logical operation that takes two binary values as input and returns a binary value as output. The operation returns a 1 in each bit position where the corresponding bits of either operand are 1, but not both.

The syntax for the Number.BitwiseXor function is:

Number.BitwiseXor(number1, number2)

Where `number1` and `number2` are the numbers to be XORed. Both arguments should be integers or numeric expressions that evaluate to integers.

Examples of Number.BitwiseXor Function

Let's look at some examples to understand how the Number.BitwiseXor function works.

Example 1:

Number.BitwiseXor(5, 3)

Output: 6

Explanation: The binary representation of 5 is 101 and the binary representation of 3 is 011. The bitwise XOR operation between these two numbers is 110, which is equal to 6 in decimal.

Example 2:

Number.BitwiseXor(15, 10)

Output: 5

Explanation: The binary representation of 15 is 1111 and the binary representation of 10 is 1010. The bitwise XOR operation between these two numbers is 0101, which is equal to 5 in decimal.

Example 3:

Number.BitwiseXor(255, 0)

Output: 255

Explanation: The bitwise XOR operation between any number and 0 is the number itself. In this case, 255 XOR 0 is equal to 255.

M Code Behind the Number.BitwiseXor Function

In Power Query, the Number.BitwiseXor function is implemented using M code. M is a functional programming language used in Power Query to transform and manipulate data. Let's take a look at the M code behind the Number.BitwiseXor function.


Number.BitwiseXor = (number1 as number, number2 as number) as number =>


binary1 = Number.ToBinary(number1),

binary2 = Number.ToBinary(number2),

padding = List.Max({Text.Length(binary1), Text.Length(binary2)}) – 1,

padded1 = Text.PadStart(binary1, padding, “0”),

padded2 = Text.PadStart(binary2, padding, “0”),

xored = List.Transform({0..padding}, each Number.FromText(Text.Middle(padded1, _, 1)) Xor Number.FromText(Text.Middle(padded2, _, 1))),

result = List.Sum(List.Transform(xored, each _ (2 ^ (_ + 1))))





The M code defines a function named Number.BitwiseXor that takes two arguments `number1` and `number2`. The function first converts the input numbers to binary using the Number.ToBinary function. It then pads the binary strings with leading zeros to make them equal in length. The function then uses the List.Transform function to iterate over each bit position in the binary strings and applies the XOR operation using the built-in Xor operator. The result is a list of 0's and 1's representing the binary representation of the XORed value.

Finally, the function uses the List.Sum function to calculate the decimal value of the XORed binary string. It does this by raising 2 to the power of the bit position and multiplying it by the corresponding binary value. The List.Transform function is used to iterate over each bit position and perform the calculation.

The Number.BitwiseXor function is a powerful tool for performing bitwise XOR operations in Power Query. In this article, we explored the M code behind the function and how it can be used to manipulate data. With a thorough understanding of the function, you can apply it to solve complex data transformation and analysis problems in Power Query.

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

Upcoming Courses

Contact Us


    Your Name (required)


    Email (required)


    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)