LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Boolean Logic Operation

Solved!
Go to solution

Hello! Recently at work I was tasked with creating code that returned something based on the state of two booleans. Something like, if boolean A and boolean B return this, if boolean A and not boolean B return something else etc. 

 

At first i figured that the best way to do it would be in a formula node with the return value being passed through a bunch of if else statements. Then i found out that booleans are not supported in formula notes, and that formula nodes are designed for writing formulas, and not for actual programming, which makes sense. 

 

I came up with two ways of doing this but I felt like that they were both pretty chunky and they would suck if I had more than 2 booleans to base my logic on. The following is the code with the two different methods.

 

Boolean Logic.PNG

 

I feel like this is one operation that would be much better to do in an if else statement in code and I'd love to see if there is a better way of doing this in LabVIEW. 

 

0 Kudos
Message 1 of 11
(5,088 Views)
Solution
Accepted by topic author PaulSammut

One typical way of doing this in LabVIEW is to combine the two booleans into an array, then use boolean array to number. This will give you a value between 0 and 3 (inclusive). Then you can use a single case statement, or you can build an array of the desired output values and use that as an index into the array, or if there's a direct mapping from the enum, you can simply cast that number to the enumeration type.

Message 2 of 11
(5,080 Views)
Solution
Accepted by topic author PaulSammut

Hi psammut,

 

build an array from your booleans. "Convert boolean array to number" to get an integer, in your case with 4 possible values. Wire this integer to the selector of a single case structure and create 4 cases ("…0", "1", "2", "3…") and put your enum constant in each case.

 

Reading the LabVIEW help on case structures might help too. 😄

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 3 of 11
(5,077 Views)
Solution
Accepted by topic author PaulSammut

I will generally build an array of the booleans and then convert to a number, using that number to index an array of possible results.

Message 4 of 11
(5,073 Views)

Thank you all!  You all replied with the same answer within a minute of each other :). 

 

-Paul

0 Kudos
Message 5 of 11
(5,066 Views)

Everyone who said Boolean Array to Number conversion and the case structure, don't you guys get confused with the case numbering? Anything beyond two elements results in non-intuitive numbering, so I've always just used Search 1D Array (for True) to make it easier to understand.

Cheers


--------,       Unofficial Forum Rules and Guidelines                                           ,--------

          '---   >The shortest distance between two nodes is a straight wire>   ---'


0 Kudos
Message 6 of 11
(5,035 Views)

The poster needs the cases where multiple items are selected, not just one of the booleans. For that case I do search 1D array for the index of True.

0 Kudos
Message 7 of 11
(5,028 Views)

@JonDieringer wrote:

The poster needs the cases where multiple items are selected, not just one of the booleans. For that case I do search 1D array for the index of True.


Ah, I see. So then binary conversion is definitely the best option.

Cheers


--------,       Unofficial Forum Rules and Guidelines                                           ,--------

          '---   >The shortest distance between two nodes is a straight wire>   ---'


0 Kudos
Message 8 of 11
(5,017 Views)

Hi James,

 

don't you guys get confused with the case numbering?

Once you set the case display to binary: No! 😄

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 9 of 11
(4,997 Views)

@GerdW wrote:

Hi James,

 

don't you guys get confused with the case numbering?

Once you set the case display to binary: No! 😄


I misunderstood the initial requirement, so binary wouldn't help too much in that case (when indices are getting up past a dozen). But in the case of OP here, binary radix is ideal! Kudos

Cheers


--------,       Unofficial Forum Rules and Guidelines                                           ,--------

          '---   >The shortest distance between two nodes is a straight wire>   ---'


0 Kudos
Message 10 of 11
(4,989 Views)