Calculating parity of a binary number


hi,

i have external device sends binary data arduino 16 bits @ time.  of these 16 bits, first bit parity of next 7 bits while last bit odd parity previous 7 bits. have idea of how in head loop , few conditionals don't quite understand how in wiring. can me?

thanks in advance :)

by "do this" assume mean check parity?

here's potential starting point:

code: [select]
unsigned thevalue; // 16-bit value external device

// i'm assuming "first bit" lsb , "last bit" msb
unsigned evenparity = (thevalue & (1<<0)) ? 1 : 0;
unsigned oddparity = (thevalue & (1<<15)) ? 1 : 0;
unsigned firstparity, secondparity, bit;

// compute parity on first set of 7 bits past parity bit
for (bit=1, firstparity=0; bit < 8; bit++) {
 firstparity ^= (thevalue & (1<<bit)) ? 1 : 0;
}

// compute parity on next set of 7 bits
for (bit=8, secondparity=0; bit < 15; bit++) {
 secondparity ^= (thevalue & (1<<bit)) ? 1 : 0;
}

// comparisons of firstparity vs. evenparity , secondparity vs. oddparity


efficiencies can realized in above more clear efficient  ;)

--
the gadget shield: accelerometer, rgb led, ir transmit/receive, light sensor, potentiometers, pushbuttons


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Calculating parity of a binary number


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums