Hey all. I'm trying to find a faster and more efficient way to evaluate a byte. I'll have some bits coming over a radio connection that will be either $00 or $FF. It is possible a certain bit error will result in perhaps a given byte received as: %00100000 instead of %00000000.

I would like to be able to parse each byte, count the number of "1" bits and the number of "0" bits, and based on whichever is higher, load the variable with either %00000000 or %11111111. This will eliminate any bit errors as long as it's not more than 3 bit errors per byte.

The only solution I've figured so far is a loop, but this process takes way too long.

This loop takes about 160 uS at 16Mhz. I'm looking for a process that can do this in under 40 uS.

Is here perhaps a more efficient assembly instruction I could use instead that would quickly evaluate this?

Code:
ErrorCheck:

    HighCount = 0
    LowCount = 0
    FOR i=0 TO 7                     
      n = testByte.0(i)              'binary value of a register
      IF n = 0 THEN
        LowCount = LowCount + 1
      ELSE
        HighCount = HighCount + 1
      ENDIF
    NEXT i

    IF HighCount >= LowCount THEN
       testByte = $FF   '255
      ELSE
      testByte = $00    'zero
     ENDIF
         
RETURN
Thanks!