LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to calculate the parity of a number?

Does anyone know of a solution that will tell whether a number has even or odd parity?  All I can think of so far is to convert the number to a hex string, break that into bytes, break the bytes into bits (haven't figured out how to do this yet) and go from there.  That seems awfully complicated for something I'd assume is used from time to time in labview?  I'm hoping someone has a simpler solution...

Thanks!
0 Kudos
Message 1 of 22
(7,726 Views)

How about this?

In this case 1 means odd, 0 would be even.  But you could work with this however you need to.  You wouldn't need to do strings and hex bytes and all of that.

Message Edited by Ravens Fan on 02-05-2007 05:40 PM

Message 2 of 22
(7,722 Views)
RavensFan's method is a good solution. It involves creating an array of booleans, changing that array into an array of numbers, adding those numbers, then performing a divide operation. It might be slightly more efficient to actually just operate on the bits themselves using functions from the Data Manipulation palette. In LabVIEW 7.1 and before, the Data Manipulation palette is in the Advanced palette, whereas in LabVIEW 8 it moved to the Numeric palette. Here you can use Rotate Right with Carry to get the value of the lsb. You can then manually cycle through all the bits and increment if the lsb is true. Finally, instead of dividing your sum by two, just use Rotate Right with Carry one more time to get the lsb. That would be your remainder for a divide by two anyway, and it should be more efficient than the divide.

Message Edited by Jarrod S. on 02-05-2007 05:26 PM

Jarrod S.
National Instruments
0 Kudos
Message 3 of 22
(7,721 Views)
Alternatively...

Message Edited by Jarrod S. on 02-05-2007 05:33 PM

Jarrod S.
National Instruments
Message 4 of 22
(7,700 Views)
Thanks to both of you for the help!  Both solutions work well and are exactly what I needed - neither was a way I had thought of to calculate parity, and were much easier than the direction I was going.  Thank you again for the help!!!
0 Kudos
Message 5 of 22
(7,671 Views)

The simplest is to use the division with "Quotient & Reminder" (numeric tools). You take your number and divide by 2. If the reminder is zero then the number is even, if the reminder is different than zero then the number is odd.

 

odd - even.PNG

0 Kudos
Message 6 of 22
(5,595 Views)

@Yodo2 wrote:

The simplest is to use the division with "Quotient & Reminder" (numeric tools). You take your number and divide by 2. If the reminder is zero then the number is even, if the reminder is different than zero then the number is odd.

 

odd - even.PNG


you are right, that this will give you the info about even or odd, but the question was whether the parity is even or odd. in this case, the number of ones in the bit representation of the number.

also this thread is like 10 years old!


If Tetris has taught me anything, it's errors pile up and accomplishments disappear.
0 Kudos
Message 7 of 22
(5,590 Views)

@RavensFan wrote:
 

How about this?

In this case 1 means odd, 0 would be even.  But you could work with this however you need to.  You wouldn't need to do strings and hex bytes and all of that.

Message Edited by Ravens Fan on 02-05-2007 05:40 PM


If only we had an "Xor Array Elements" function, then we could do it like this:

xora.jpg

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

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

 

 

0 Kudos
Message 9 of 22
(3,727 Views)

@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.

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 10 of 22
(3,711 Views)