LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

4-bit numbers sanity check

Solved!
Go to solution

Hi,

 

maybe late, but just some simple math:

check.png

(There should be some sanity checks like limiting the input to 8 elements and InRangeAndCoerce to 0…15.)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 11 of 21
(1,369 Views)

@GerdW wrote:

 

maybe late, but just some simple math:

 

(There should be some sanity checks like limiting the input to 8 elements and InRangeAndCoerce to 0…15.)

Bitwise operations are probably more appropriate than "math". Here's a quick draft (same result). (I added the "AND 1111" as partial "sanity check". Not needed if the input is clean)

 

processNibblesB.png

Not sure if it would be better to convert the array to U32 right before the loop. 

Message 12 of 21
(1,362 Views)

Hi Christian,

 

my intention behind "simple math": on modern CPUs integer operations are mostly executed at same speed (ADD, MUL, AND, OR, SHIFT), so the "×16" and "+U8" is the same speed as "ASL by 4" followed by "OR U8"…

 

Many people "understand" simple math better than boolean/shift operations. (I do prefer boolean operations…)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 13 of 21
(1,355 Views)

@Kyle97330 wrote:

Here's my "Extreme minimalist" edition.  Didn't benchmark it though.  Should scale to any input array size so long as it's even (if not, it'll need to be padded or it clips the last one).

4 bits to byte array.png


Not quite "minimalist".  You don't need the FOR loop.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 14 of 21
(1,343 Views)

I Did not benchmark it but I thought the type cast was a good idea.

 

U32_To_U8.png

 

The swap- byte and swap-words can be used if there is an edianess concern.

 

 

Edit:

 

Yeah I know, you wanted four bit bytes.. oh well! A KL-10 used to support variable length bit fields natively but I do think LV was ever ported to run on Kl-10s Smiley Tongue

Ben 

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 15 of 21
(1,335 Views)

@GerdW wrote:

 

Many people "understand" simple math better than boolean/shift operations. (I do prefer boolean operations…)


I would argue that this "special" case of a multiplication is harder to understand than a bit shift, at least for people outside computer sciences). Most multiplications don't result in a bit shift, just the ones with integer powers of two. 😉

0 Kudos
Message 16 of 21
(1,318 Views)

Hi Christian,

 

the multplication becomes more intuitive when we write the constant in hex representation "* 0x10"! 😄

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 17 of 21
(1,313 Views)

Thank you for all the suggestions, I learned from all of them! Jamiva's 16x16 lookup table turned out to be really fast. For an 8192 x 1000 input array, it took me about 3.8 seconds per run using my original method. Using the typecasting actually increased the time to 4.8 seconds per run (though it looked much more elegant). Using the Logical Shift and OR as Kyle did took about 400ms, and using the lookup table was just a hair faster at 380ms.

Message 18 of 21
(1,288 Views)

@Gregory wrote:

For an 8192 x 1000 input array, it took me about 3.8 seconds per run using my original method. 


My impression was that this is a one-shot 8 byte deal, so performance should not matter. But yes, this is one of the few scenarios where "going green" (i.e. detour via booleans :D) is not a good option.

0 Kudos
Message 19 of 21
(1,279 Views)

Just for fun, here's yet another "no loop" version. same result an the original. For sanity, one might want to add a "AND 1111" as a first step. 😄 (Of course if the final data for the device is a binary string, we might as well cast the U32 directly to a string.)

 

NibbleProcessor2.png

(Again, Gerd could use a multiply and a different middle array constant (x1, x10, x100, etc.)  :D)

 

Message 20 of 21
(1,274 Views)