Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
03/24/06 07:53
Read: times


 
#112938 - How about a portable solution?
Responding to: ???'s previous message

I pondered this issue recently. Whilst it is very straightforward in assembler as mentioned, it doesn't make for very portable code! I trolled the web for an elegant solution but there was nothing that grabbed me. Simply, parity is the addition of all the bits - the result is a bit. Depending on if you invert this bit, you get even or odd parity. So in general 'c', one would get:

char do_parity(char val)
{
char a,parity,tmp;

tmp = val; //get a copy to play with
parity = 0; //start with parity = 1 for odd parity result
for(a=0; a<7;a++)
{
if (tmp & 0x01)
{
parity ^= 0x01; //flip the parity bit for each '1' bit
}
tmp>>=1; //shift next bit
}
return parity;
}

Not super efficient is it? Hopefully the compiler is smart enough to figure that you are calculating parity and substitutes the more optimum code!!



List of 30 messages in thread
TopicAuthorDate
odd parity problem            01/01/70 00:00      
   In Raisonance and Keil            01/01/70 00:00      
      OR            01/01/70 00:00      
         How about a portable solution?            01/01/70 00:00      
            calculating parity..            01/01/70 00:00      
            portable my ...            01/01/70 00:00      
         Or another portable solution?            01/01/70 00:00      
         And one more fast method            01/01/70 00:00      
            fast?            01/01/70 00:00      
               Yep            01/01/70 00:00      
                  Bag of Tricks            01/01/70 00:00      
                     bag of tricks..            01/01/70 00:00      
                        Good link. Thanks.            01/01/70 00:00      
                     Collecting            01/01/70 00:00      
                        ciontradictory request            01/01/70 00:00      
                           One confused puppy            01/01/70 00:00      
                              not confused            01/01/70 00:00      
                                 I/O and parity for '51 belongs in Chat            01/01/70 00:00      
                                    If memory parity were implemented, I sur            01/01/70 00:00      
                                       Memory -- expand your thinking a little            01/01/70 00:00      
                                          eeprom would be I/O            01/01/70 00:00      
                                             Memory plus software parity            01/01/70 00:00      
                                             8051 does calculate parity!            01/01/70 00:00      
                                                ALU versus I/O            01/01/70 00:00      
                  no such thought            01/01/70 00:00      
                     My two cents            01/01/70 00:00      
                        Choosing by architecture            01/01/70 00:00      
            thanks:) 18 Bytes each!            01/01/70 00:00      
               "Old" Europe            01/01/70 00:00      
   Simple method            01/01/70 00:00      

Back to Subject List