PHP Calculate Parity

AceInfinity

Emeritus, Contributor
Joined
Feb 21, 2012
Posts
1,728
Location
Canada
Here's a class that I wrote which encapsulates a generic interface for calculating parity of a value from a certain datatype. This has not been tested extensively for 64 and 16 bit however, but from my quick tests it seems to be okay for 16 bit.

Here is a nice couple links:
Easy Parity calculation
AVR Freaks :: View topic - Easy method to calculate even-parity for 16-Bit

And for reference:
pSeries and AIX Information Center

Code:
[NO-PARSE]// main.cpp
#include <iostream>
#include <climits>

static const bool parity_table[256] = 
{
	#define P2(n) n, n^1, n^1, n
	#define P4(n) P2(n), P2(n^1), P2(n^1), P2(n)
	#define P6(n) P4(n), P4(n^1), P4(n^1), P4(n)
	P6(0), P6(1), P6(1), P6(0)
};

template <typename T>
class ParityEvaluate
{
public:
	ParityEvaluate(const T n) : val(n) {}
	operator bool ()
	{
		T r = val;
		switch (sizeof(T)*CHAR_BIT)
		{
			case 8: // 8 bit
				r = parity_table[r];
				break;
			case 16: // 16 bit
				r ^= r >> 8;
				r ^= r >> 4;
				r = parity_table[r & 0xF];
				break;
			case 32: // 32 bit
				r ^= r >> 16;
				r ^= r >> 8;
				r = parity_table[r & 0xFF];
				break;
			case 64: // 64 bit
				r ^= r >> 24;
				r ^= r >> 16;
				r = parity_table[r & 0xFFFF];
				break;
		}
		return r;
	}
private:
	T val;
};

int main()
{
	/*
	** Ex:
	** 8 bit: 7  = 00000111
	** 8 bit: 20 = 00010100
	**
	** Parity calculation is based off of # of set 1 bits
	** in the binary representation of a value
	*/

	unsigned char n = 117;
	std::cout << "Number: " << static_cast<unsigned>(n) << std::endl;
	ParityEvaluate<unsigned char> odd_parity(n);
	std::cout << "Parity: " << (odd_parity ? "Odd" : "Even") << std::endl;
	std::endl(std::cout);
	return 0;
}[/NO-PARSE]

There is also other terminology that applies to such calculations. Here is a link that outlines what a parity bit means: Parity bit - Wikipedia, the free encyclopedia

:beerchug2:
 
There is also the parity flag stored in the EFLAGS register, which is set if the last operation in binary was even or odd.

Code:
1: kd> [COLOR=#008000]r @pf[/COLOR]
pf=[COLOR=#ff0000]1[/COLOR]
 
Attempting a new method.

Code:
[NO-PARSE]#include <iostream>
#include <cstdint>

uint32_t f_parity(uint32_t v)
{
	uint32_t p = 0;
	__asm
	{
		mov eax, v   // move value to eax register
		or eax, eax  // logical or on eax register with saved result (eax |= eax)
		pushfd       // push EFLAGS register contents to stack
		pop eax      // grab EFLAGS register value from stack
		shr eax, 2   // shift right twice
		and eax, 1   // grab the PF bit
		mov p, eax   // move eax register to variable p
		xor eax, eax // clear eax register
	}
	return p;
}

int main_method(void)
{
	uint32_t n = 23784;
	std::cout << f_parity(n);
	std::endl(std::cout);
	return 0;
}[/NO-PARSE]

Taken from the EFLAGS register by using the pushfd instruction. Keep note that registers are 32 bits though. They can be split up, but this method will only work for 32 bit.
 
Last edited:

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top