How to check if a bit is set in PowerApps?

360 views Asked by At

How do I check if a particular bit is set on an integer value within a PowerApp? There doesn’t seem to be a built-in operator or function for bit manipulation.

As I do need this for quite few operations, using an external / Custom Connector is probably too expensive for me.

2

There are 2 answers

0
carlosfigueira On BEST ANSWER

To find if a bit b of a number val is set, you can use an expression like the one below:

RoundDown(Mod(val,Power(2,b+1))/Power(2,b),0)

An example of this expression is shown below:

enter image description here

0
J-M On

Formula

Mod(Trunc(Number / (2 ^ Bit)), 2) = 1

This expression will be true if Bit flag (counting from zero) for the value Number is set. Otherwise false.

Examples

32 (00100000)

Number = 32 Bit = 0 : false
Number = 32 Bit = 4 : false
Number = 32 Bit = 5 : true

33 (00100001)

Number = 33 Bit = 0 : true
Number = 33 Bit = 4 : false
Number = 33 Bit = 5 : true

Reusability

If you are going to use it often in your app, you might want to register it as a custom function for easier use