From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to calculate the parity of a number?


@toojer wrote:

I was doing a quick search and found this stream. I realize it's a few years old.

I think some of the answers are great. The easiest way, in my opinion, to implement this is to do the following.

 

ParityCalculation.png


Depends what you mean with easiest. It involves creating an array of booleans. While not a huge thing, it certainly costs extra performance as a new memory block needs to be allocated, so it's not easiest in terms of CPU load. For that you want to use the solution in Message 4 from Jarrod. And in terms of complexity it is definitely not really more difficult to understand.

Rolf Kalbermatter
My Blog
0 Kudos
Message 11 of 22
(1,318 Views)

@Yamaeda wrote:

@toojer wrote:

I was doing a quick search and found this stream. I realize it's a few years old.

I think some of the answers are great. The easiest way, in my opinion, to implement this is to do the following.

 

ParityCalculation.png


Just do an AND 1 and the result is 1 for odds and 0 for evens. 😉 No need to convert the number to a 32 byte array and loop through it.


That will tell you if the number itself is odd or even, but not if the number of bits in the binary representation of the number is odd or even. Someone else already did a similar mistake earlier in the thread.

Rolf Kalbermatter
My Blog
0 Kudos
Message 12 of 22
(1,317 Views)

How's this:
p.png

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 13 of 22
(1,298 Views)

Same issue about creating an array of 8 bytes for the booleans. Not a huge performance hit but it is creating a LabVIEW handle which is a memory manager call, only to throw it away again immediately. Numeric to Boolean array in LabVIEW is even more costly than a Typecast and absolutely not a free operation unless you program in FPGA!!!

Rolf Kalbermatter
My Blog
0 Kudos
Message 14 of 22
(1,289 Views)

This isn't about whether the number is odd or even, but whether the number of bits in the binary number that are "1" are odd or even.

 

EDIT:  Sorry for responding.  Because message #10 that I was responding to was at the bottom of page 1, I didn't see the few threads before mine that were already pointing the out the mistake in message #10.

0 Kudos
Message 15 of 22
(1,277 Views)

I don't think what you wrote here is correct.  This will give the even or odd parity of the 32-bit boolean array.  The result of this is the parity bit that you append to the word. Giving the result of the even or odd parity bit.

0 Kudos
Message 16 of 22
(1,268 Views)

I don't think what you wrote here is correct.  This will give the even or odd parity of the 32-bit boolean array.  The result of this is the parity bit that you append to the word. Giving the result of the even or odd parity bit.

0 Kudos
Message 17 of 22
(1,263 Views)

OK, for just an 8-bit number, how about a LUT:

p2.png

(and this time with a correct label on the output!)

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 18 of 22
(1,241 Views)

@paul_cardinale wrote:

OK, for just an 8-bit number, how about a LUT:

p2.png

(and this time with a correct label on the output!)


Probably fastest. But will require a 256 byte array for the booleans. You seem to have a high aversion to boolean operations. 😁

Rolf Kalbermatter
My Blog
0 Kudos
Message 19 of 22
(1,233 Views)

Divide and conquer is how I roll:

 

Parity.png

 

For a little extra speed I will unroll - the loops that is.

Message 20 of 22
(1,169 Views)