Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Implementing parity calculation in C language

Status
Not open for further replies.

subdural

Junior Member level 1
Joined
Aug 21, 2004
Messages
19
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Location
Neutral Zone
Activity points
173
parity calculation

Hi!

I'm newbie. I have the main.c source code as below. I'd to know how to program source the parity of 32 bit word calculation then determine whether the number is even (parity is zero) or the number is odd (parity is 1) in C language. Says that the parity program is difference source code called parity.c source code.Thanks


main.c source code
----------------------

main()
{ int attempt, in, out;
attempt = 1;
while (attempt < 7) {
printf("Enter a 32-bit value in hex:");
scanf("%X", &in);
if(parity(in))
printf("Value %X has odd parity\n", in);
else
printf("Value %X has even parity\n", in);
attempt++;
}
}

-------------------------------------------------------------------

after compile

$ gcc main.c parity.s -o test.exe

Expected results after gcc run as below

$ gcc run test.exe
Enter a 32-bit value in hex: ffffffff
Value ffffffff has even parity
Enter a 32-bit value in hex: fffffffe
Value fffffffe has odd parity
Enter a 32-bit value in hex: 0
Value 00000000 has even parity
Enter a 32-bit value in hex: 1
Value 00000001 has odd parity
Enter a 32-bit value in hex: ffff0000
Value ffff0000 has even parity
Enter a 32-bit value in hex: 55554555
Value 55554555 has odd parity
Enter a 32-bit value in hex: 12345678
Value 12345678 has odd parity
 

odd parity calculation

After searching Google for a few seconds:
**broken link removed**
 

    subdural

    Points: 2
    Helpful Answer Positive Rating
parity bit calculation

Simple, Just count the number of ones in the binary number.

0x01 = 00000000 00000000 00000000 00000001 = 1 bit
0x03 = 00000000 00000000 00000000 00000011 = 2 bits
0x0F = 00000000 00000000 00000000 00001111 = 4 bits

if no of one bits are even number, then it is off even parity.
if no of one bits are odd number, then it is off odd parity.

try this code

Code:
int parity(unsigned long ino)
{
	int noofones = 0;
	unsigned long mask = 0x00000001ul; /* start at first bit */

	while(mask != 0) /* until all bits tested */
	{
		if(mask & ino) /* if bit is 1, increment noofones */
		{
			noofones++;
		}
		mask = mask << 1; /* go to next bit */
	}

	/* if noofones is odd, least significant bit will be 1 */

	return (noofones & 1); 
}

hope it helps.

Cheers
idlebrain
 

    subdural

    Points: 2
    Helpful Answer Positive Rating
fast parity calculation

Hi Idlebrain & echo!

You do good job. I'll try to link this source code.
 

calculate parity bit

A more efficient function:

unsigned char parity(unsigned long ino)
{
unsigned char noofones = 0;

while(ino != 0)
{
noofones++;
ino &= (ino-1); // the loop will execute once for each bit of ino set
}

/* if noofones is odd, least significant bit will be 1 */

return (noofones & 1);
}
 

even parity calculation

Clever, but I'd call that a "variable efficiency" solution. ;)
Looks like anywhere from 2 to 98 ALU operations.
The bitops2.c function uses 11 operations and no branching.
 

bitops2.c

svicent said:
A more efficient function:

looks nice, as said by echo47, this is variable efficient.

Cheers
idlebrain
 

calculate odd parity

Using the C51 K*E*I*L C compiler, I have compiled the two versions of parity function.

I have changed the return value of parity1 from "int" to "unsigned char", to make it more efficient.
After 150,000 calls to function, the results are:

Cycles with first version:
min 2900
max 2917
avg 2908

Cycles with second version:
min 47
max 1050
avg 545

First version code size is approximately 30 bytes larger than second version.
 

parity c

Great job svicent :D

It is very obvious, as number of loops depends on the number of 1s. The minimum cycles will be less.

Cheers
idlebrain
 

calculate parity c

The bitops2.c function won't be very fast if the CPU lacks a barrel shifter.
 

parity bit c++

It is a known fact that the 8051 CPU lacks a barrel shifter.
On the other hand, the operation
ino = ino & (ino-1);
does not need a barrel shifter.
 

parity function c

This form of parity is essentially a useless dinosaur on modern computing devices. It would be faster to send an entire parity byte which could be used to detect more than 1 bit errors, calculate the partiy equivilancy of any number bytes and determine if even one of those bytes in the packet was out of wack. Send a CRC byte of the parity byte with the parity data and you have a virtually impossible to 'noise out' data transmittion method. The overhead on even 8 bit processors is low. Please note Parity bytes and the Parity bit are VERY Different things.
https://www.pcguide.com/ref/hdd/perf/raid/concepts/genParity-c.html
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top