LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

must be an easier way...

Solved!
Go to solution

I am both amused and horrified simultaneously right now.  I enjoy a good bit twiddle, but it only seems to make sense in cases like the FFT where the code will be run tens of millions of times.  If you shave a microsecond off the execution, how many times will it have to run to make up for the time it takes you to say "What the f.." next time you look at it?  Not to mention the time it took to type in the necessary comments.  Let's say you no longer want one of the booleans in the second byte, you have to calculate the mask value and change all of the downstream shifts.

 

I thought you wanted an "easier" way.  

 

(I feel like my evil twin typing this.  Normally I am a huge fan of clever and obscure solutions but the forum has had a bad influence and I now start asking things like "What happens to the poor fellow who inherits this code in a couple of years?")

 

On second thought, leaving behind code that nobody else can understand is a good way to help them realize your genius once you are gone, right?  Twiddle away!

 

 

 

Message 21 of 28
(1,303 Views)

Hi for(imstuck),

 

over night I got an inspiration:

As you interpret some status bits from some device you could do this:

- pick the 4 status bytes from the data stream

- typecast the 4-byte-array to a cluster of 32 bits -> et voila, you're ready!

 

For this to work you have to setup your status cluster just a little bit different:

Define a cluster with 32 booleans (as strict typedef?!). Label them as you did now, but include the unused bits (labeled as "unused1", "unused2"...). You can even hide the unused bits for better look of the front panel! And you access the relevent information (as you should do right now) by simple unbundle by name functions...

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 22 of 28
(1,285 Views)

Darin.K wrote:

On second thought, leaving behind code that nobody else can understand is a good way to help them realize your genius once you are gone, right?  Twiddle away!


Clever does not need to be obscure, and I actually think that my version is very readable to anyone who ever did bitwise operations. I even think it is significantly easier to read than e.g. Gerd's above: No orange wires, no "2^x -1" head scratcher, no multiple SRs. Bitwise OR is probably more natural than "add" for this. 😮

 

Your version is a more literal implementation of the problem, but it's again complicated by the unecessary presence of the initial array subset. If it's not there, it does not need to be figured out by the next programmer. 😉

 

Sure, things need to be documented, but that's true for all solutions presented here. I agree, performance for this problem is only a secondary consideration. Still, I do the bulk operation in a singe U32 (4 bytes) while you successively grow an array to 21 bytes. This is over 5x more and requires successive datacopies with each iteration of the loop.

 

Honing the programming skills on miniatures likes this IS important. This way we don't need to learn new techniques once we need to deal with larger data structures, where the coding choice make a difference between 10ms and 10s, or between "works great" vs. "out of memory". It is a matter of scaleability. 😄 If you only want to learn one algorithm to do a certain operation, learn the one that works under all conditions. 🙂

 

For better documentation and code readability,  I would probably still do a few things slighly differently. For example the mask diagram constant could be formatted in binary so they show as [0, 111111, 111111, 111111, 111]. Also notice that I always show one extra greyed element to document the array size of the diagram constants. If you only show the actual elements, you are never really sure how big the array really is by just looking at the diagram image. (Container size has nothing to do with array size!) Again a potential source for errors. If we make the code more self-documenting, fewer diagram comments are needed.

 

The documentation cold be a very simple, showing the program purpose with the aid of color. e.g.

 


Extract the 21 bits shown in red from an U8 array and cast to a cluster of 21 booleans for display.

[xxxxxxxx,xxxxxxxx,xxxxxxxx,xxxxxxxx,xxxxxxxx,xxxxxxxx,xxxxxxxx,...]

The order of the bits in the output should be low to high for each source octet.


Message 23 of 28
(1,276 Views)

Hi altenbach,

 

"No orange wires, no "2^x -1" head scratcher, no multiple SRs. Bitwise OR is probably more natural than "add" for this."

 

As I also wrote: the 2^x part was only there for showing the concept and could be easily replaced by an array constant, but to create a mask of x "TRUE" bits is just 2^x-1... Also at this point I was expecting the possibility for different bit masks therefor using more than one SR. And yes, an OR is probably more natural, but the same as an ADD as long as bit ranges aren't overlapping. And well, I recently wrote a similar bitpacking routine, so I surely included to less documentation in the snippet Smiley Wink

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 24 of 28
(1,266 Views)

altenbach wrote:
example the mask diagram constant could be formatted in binary so they show as [0, 111111, 111111, 111111, 111]. Also notice that I always show one extra greyed element to document the array size of the diagram constants. If you only show the actual elements, you are never really sure how big the array really is by just looking at the diagram image. (Container size has nothing to do with array size!) Again a potential source for errors. If we make the code more self-documenting, fewer diagram comments are needed.

 


Very good point. Speaking of which, is there a way to grey out elements? For instance if I add one too many? I have been looking for a way to do this for, oh, about a year! I always end up recreating the array.

0 Kudos
Message 25 of 28
(1,241 Views)

Darin.K wrote:

 

 

(I feel like my evil twin typing this.  Normally I am a huge fan of clever and obscure solutions but the forum has had a bad influence and I now start asking things like "What happens to the poor fellow who inherits this code in a couple of years?")

 

On second thought, leaving behind code that nobody else can understand is a good way to help them realize your genius once you are gone, right?  Twiddle away!

 

 

 


I like this reply Darrin, after all, once I got my comments to a point where I thought they would be understood, they took up way more diagram space than the code! However, to each his own, there is more than one way to skin a cat yada yada. I'll let ya know if someone comes yelling at me in a year or two!

0 Kudos
Message 26 of 28
(1,238 Views)

Darin.K wrote:

I am both amused and horrified simultaneously right now. ...

 

(I feel like my evil twin typing this.  Normally I am a huge fan of clever and obscure solutions but the forum has had a bad influence and I now start asking things like "What happens to the poor fellow who inherits this code in a couple of years?")

 

On second thought, leaving behind code that nobody else can understand is a good way to help them realize your genius once you are gone, right?  Twiddle away!

 

 

 


Button hanging in one of the cubes my wife used to inhabit read

 

"Good prgrammers don't document.

If it was hard to write,

it should hard to understand".

 

(insert sarcasm emoticon here).

 

Ben

 

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 27 of 28
(1,236 Views)

for(imstuck) wrote:

Very good point. Speaking of which, is there a way to grey out elements? For instance if I add one too many? I have been looking for a way to do this for, oh, about a year! I always end up recreating the array.


Right click on the element you want to remove.  Data Operations >> Delete Element.

Message 28 of 28
(1,221 Views)