취소
다음에 대한 결과 표시 
다음에 대한 검색 
다음을 의미합니까? 

Boolean Logic Operation

해결 완료!
솔루션으로 이동

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 포인트
1/11 메시지
7,948 조회수
솔루션
승인자 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.

2/11 메시지
7,940 조회수
솔루션
승인자 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
3/11 메시지
7,937 조회수
솔루션
승인자 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.

4/11 메시지
7,933 조회수

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

 

-Paul

0 포인트
5/11 메시지
7,926 조회수

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 포인트
6/11 메시지
7,895 조회수

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 포인트
7/11 메시지
7,888 조회수

@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 포인트
8/11 메시지
7,877 조회수

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
9/11 메시지
7,857 조회수

@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 포인트
10/11 메시지
7,849 조회수