The simFlight Network Forums

# Checking if an Offset bit is set in LUA script

## Recommended Posts

Hi All,

I am sure that there is an obvious answer to this that I have missed. I need to check if a specific bit within an Offset is set.

Specifically, Offset 3122. (Radio audio switches)  I need to check if the bit in location 2^5 (COM receive both) is set.

I am new to working with bits so I apologize if the solution is obvious. I have read Pete's explanations on understanding bits, which were all very helpful. I have found the ipc library functions to work with Offset values to clear specific bits, set specific bits, toggle, etc, but I cannot find a similar function to evaluate the value at a current bit location using a mask. I've read threads to understand how to set specific bits using decimal values. I can't figure out, however, if there is a way to use math to determine if a specific bit is set.

I've also reviewed the logic functions within the LUA library and cant figure out how I could use these to achieve my goal. I fear that there is some obvious way to perform this task that I'm missing.

The only solution that my small brain could come up with is:

1) Read the 3122 Offset value.
2) Write the value into an Offset that is open to my temporary use.
3) Use the ipc.clearbitsUB function on this temporary Offset to clear all bits except for 2^5 using an appropriate mask.
4) Read the updated value from this temporary Offset location.
5) The decimal value should either be 32 or 0.  A value of 32 tells me that the bit at 2^5 is set, 0 tells me that it was not set.

Can someone enlighten me on the simple solution for checking if a specific bit is set? Thank you in advance!

Prepar3D, FSUIPC 4.957

##### Share on other sites

30 minutes ago, Phouillon said:

Specifically, Offset 3122. (Radio audio switches)  I need to check if the bit in location 2^5 (COM receive both) is set.

Two ways.

If you want to just wait and detect when it is set or cleared, use event.offsetmask with the mask set to 2^5 (i.e. 32, or 0x0020).

If you just want to read it and test the bit, just read it then using logic.And to isolate bit 2^5 (i.e. "and" it with 32 or 0x0020). If the result s non-zero the bit is set.

I suggest you explore the library facilities a bit further than the first few pages, and maybe look at some of the many examples.

Pete

##### Share on other sites

Thank you Pete. The solution for my situation is the logic.And approach. Now that I understand how you can isolate a bit within the logic.And function, it makes perfect sense.

I've spent enough time reading this forum to realize that you handle just about every post yourself and I can imagine the frustration of handling questions that should be solved by reading the documentation. Knowing this, please trust that I spent multiple hours reading the library documents, end-to-end to avoid adding to your load.

For some reason my brain was not visualizing how the logic.And function could be used to isolate and evaluate a specific bit. Your advice was extremely valuable and is appreciated!

## Create an account

Register a new account