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

Understanding the Bitwise NOT Operator

Before we dive into the M code behind the Number.BitwiseNot function, let’s first understand what the bitwise NOT operator does. The bitwise NOT operator is represented by the tilde (~) symbol and is used to invert the bits of a number. In other words, it converts all 0s to 1s and all 1s to 0s.

For example, if we apply the bitwise NOT operator to the number 5, which is represented in binary as 00000101, we get the result 11111010, which is equivalent to the decimal value -6. This is because the first bit in a binary number represents the sign, and a 1 in this position indicates a negative number.

The Number.BitwiseNot Function

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

Number.BitwiseNot(number as nullable number) as nullable number

This function takes a single argument, which is the number that we want to perform the bitwise NOT operation on. The argument can be any nullable numeric value, including integers, decimals, and null values. The function returns a nullable numeric value that represents the result of the operation.

To use the Number.BitwiseNot function in Power Query, we simply call the function and pass in the number that we want to operate on. For example, if we want to perform a bitwise NOT operation on the number 5, we would use the following code:


This would return the result -6, as we saw earlier.

Applications of the Number.BitwiseNot Function

The Number.BitwiseNot function can be used in a variety of scenarios, such as:

Flipping Bits

One common use case for the Number.BitwiseNot function is to flip the bits of a binary number. For example, if we have the number 10101010, we can use the bitwise NOT operator to flip all of the bits and get the result 01010101. This can be useful in cryptography, data compression, and other applications where binary data needs to be manipulated.

Logical Operations

Another use case for the Number.BitwiseNot function is to perform logical operations on binary data. For example, we can use the bitwise NOT operator to invert the bits of a number and then perform a logical AND or OR operation with another number. This can be used to mask or filter out certain bits in the data.

Data Validation and Error Checking

The Number.BitwiseNot function can also be used for data validation and error checking. For example, if we have a binary number that represents a set of flags or permissions, we can use the bitwise NOT operator to ensure that all bits are set to either 0 or 1. If any other value is returned, we know that there is an error in the data.

The Number.BitwiseNot function in Power Query M language is a powerful tool for performing bitwise NOT operations on numeric data. This function can be used to manipulate binary data, perform logical operations, and validate data. By understanding the M code behind this function and its applications, we can take advantage of its capabilities and enhance our data processing workflows.

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

Upcoming Courses

20-22 Nov 23 (London or Online)

Contact Us


    Your Name (required)


    Email (required)


    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)