LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

VI of the Day (10/27/2009) - Boolean to (0,1)

Sticking with the efficiency theme, this one has me (and a few others) scratching their heads, "Boolean to (0,1)".  Seems pretty straightforward, boolean in and either a 0 or 1 out.  What is interesting is the choice of I16 for the representation of 0 or 1.  It isn't so much that you are now using 16-bits to hold 1 bit of information, but how often do you use the I16 data type?  If you are indexing an array or loop you are using I32 (or coercing), if you are working with byte arrays you use U8.  Which now makes me think, why the heck do you use a signed representation for 0/1?

 

BooleanTo01.png 

 

My guess is that in the old days booleans were stored in two bytes (it must have seemed like a good idea at the time), and this is a remnant.  What we are left with is a potentially useful function that we have to either follow with a conversion, replace with Select or Type Cast or live with a coercion.

 

I believe there is a movement afoot in the idea exchange to get this changed. 

 

VIOTD groundrules here

Message 1 of 19
(3,995 Views)
Actually, Booleans are stored as 8-bit values. It used to be that they were stored as 1-bit values. But that was a loooooong time ago. This little tidbit is what makes this little trick possible.
Message 2 of 19
(3,984 Views)
Could never figure out why NI made this output I16. Your explanation seems reasonable.
PaulG.
Retired
0 Kudos
Message 3 of 19
(3,979 Views)
Hmmm... You know, now that I think about it you may be right about the 16-bit storage. I could swear, though, there was a 1-bit storage of something at one point. Of course, I may be confusing it with another language.
0 Kudos
Message 4 of 19
(3,972 Views)

smercurio_fc wrote:
Actually, Booleans are stored as 8-bit values. It used to be that they were stored as 1-bit values. But that was a loooooong time ago. This little tidbit is what makes this little trick possible.
 
Booleans are now one byte in or out of arrays.  Used to be they were 2 bytes outside of an array and 1-bit inside of an array.  The 1-bit representation is still very useful, especially when dealing with image masks.  The one byte representation is very useful for code obfuscation, if nothing else.  Is there anything else?   

 

0 Kudos
Message 5 of 19
(3,968 Views)

I use this guy when reading an error cluster or other boolean to a 3-way LED.  Not sure if this is the best approach, but ?1:0 saves me a Select and some wiring.  The 3way LED is a 3 item pict ring with images for off, green, and red.

 

 

Boolean to 1_0.png (LV2009f2)

-Barrett
CLD
0 Kudos
Message 6 of 19
(3,961 Views)

Darin.K wrote:
Booleans are now one byte in or out of arrays.  Used to be they were 2 bytes outside of an array and 1-bit inside of an array. 

Ah-hah! That was it. I knew there was a 1-bit storage of something in the dark, dreaded past. Your memory is clearly better than mine.  Smiley Very Happy

0 Kudos
Message 7 of 19
(3,960 Views)

smercurio_fc wrote:
This little tidbit is what makes this little trick possible.

I can't figure out how that works. It actually seems pretty scary that others were able to do the same thing, but with other strings coming out. Can anyone explain that to me?

-Randy
-=--=-=-=-=-=-=-
Nothing like a good dose of LabVIEW to cure what ails ya'.
0 Kudos
Message 8 of 19
(3,898 Views)

Hi Randy,

 

two facts:

1) a boolean is (nowadays) stored as 8bit entity internally

2) any value different than zero is TRUE

 

Do you need more hints?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 9 of 19
(3,891 Views)

Darin.K wrote:

Sticking with the efficiency theme, this one has me (and a few others) scratching their heads, "Boolean to (0,1)".  Seems pretty straightforward, boolean in and either a 0 or 1 out.  What is interesting is the choice of I16 for the representation of 0 or 1.  It isn't so much that you are now using 16-bits to hold 1 bit of information, but how often do you use the I16 data type?  If you are indexing an array or loop you are using I32 (or coercing), if you are working with byte arrays you use U8.  Which now makes me think, why the heck do you use a signed representation for 0/1?

 

BooleanTo01.png 

 

My guess is that in the old days booleans were stored in two bytes (it must have seemed like a good idea at the time), and this is a remnant.  What we are left with is a potentially useful function that we have to either follow with a conversion, replace with Select or Type Cast or live with a coercion.

 

I believe there is a movement afoot in the idea exchange to get this changed. 

 

VIOTD groundrules here


The real reason can only be provided by someone who was in the meeting can tell us for sure but I'll speculate...

 

Since LV is cross-platform capable, ther various branching operations supported by the mulitude of patforms LV was targeted at was taken into concideration. A common machine operation is to "branch if negative". By writing all bits as true or false when a branch if negative is executed the MSB of the I16 is set making it negative.

 

So I suspect the "lowest common denomenator" across platforms was a branch if negative.

 

Rolf (if he is listening) is much more familair with the platform abstraction layer than I so maybe he could shed more than speculative light on this Q.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 10 of 19
(3,889 Views)