public static BinaryMessage checkAndCorrect(BinaryMessage message,int startIndex){
  boolean parityError=message.cardinality() % 2 != 0;
  int syndrome=getSyndrome(message,startIndex);
  if (syndrome == 0) {
    if (parityError) {
      message.flip(startIndex + 23);
    }
    message.setCRC(CRC.PASSED);
    return message;
  }
  int original=message.getInt(0,22);
  int index=-1;
  int syndromeWeight=3;
  int errors=0;
  while (index < 23) {
    if (index != -1) {
      if (index > 0) {
        message.flip(index - 1);
      }
      message.flip(index);
      syndromeWeight=2;
    }
    syndrome=getSyndrome(message,startIndex);
    if (syndrome > 0) {
      for (int i=0; i < 23; i++) {
        errors=Integer.bitCount(syndrome);
        if (errors <= syndromeWeight) {
          message.xor(12,11,syndrome);
          message.rotateRight(i,startIndex,startIndex + 22);
          if (index >= 0) {
            errors++;
          }
          int corrected=message.getInt(0,22);
          if (Integer.bitCount(original ^ corrected) > 3) {
            message.setCRC(CRC.FAILED_CRC);
            return message;
          }
          message.setCRC(CRC.PASSED);
          return message;
        }
 else {
          message.rotateLeft(startIndex,startIndex + 22);
          syndrome=getSyndrome(message,startIndex);
        }
      }
      index++;
    }
  }
  message.setCRC(CRC.FAILED_CRC);
  return message;
}

public static BinaryMessage checkAndCorrect ( BinaryMessage message , int startIndex ) { boolean parityError = message . cardinality ( ) % 2 != 0 ; int syndrome = getSyndrome ( message , startIndex ) ; if ( syndrome == 0 ) { if ( parityError ) { message . flip ( startIndex + 23 ) ; } message . setCRC ( CRC . PASSED ) ; return message ; } int original = message . getInt ( 0 , 22 ) ; int index = - 1 ; int syndromeWeight = 3 ; int errors = 0 ; while ( index < 23 ) { if ( index != - 1 ) { if ( index > 0 ) { message . flip ( index - 1 ) ; } message . flip ( index ) ; syndromeWeight = 2 ; } syndrome = getSyndrome ( message , startIndex ) ; if ( syndrome > 0 ) { for ( int i = 0 ; i < 23 ; i ++ ) { errors = Integer . bitCount ( syndrome ) ; if ( errors <= syndromeWeight ) { message . xor ( 12 , 11 , syndrome ) ; message . rotateRight ( i , startIndex , startIndex + 22 ) ; if ( index >= 0 ) { errors ++ ; } int corrected = message . getInt ( 0 , 22 ) ; if ( Integer . bitCount ( original ^ corrected ) > 3 ) { message . setCRC ( CRC . FAILED_CRC ) ; return message ; } message . setCRC ( CRC . PASSED ) ; return message ; } else { message . rotateLeft ( startIndex , startIndex + 22 ) ; syndrome = getSyndrome ( message , startIndex ) ; } } index ++ ; } } message . setCRC ( CRC . FAILED_CRC ) ; return message ; }
performs error detection and returns a corrected copy of the 24 - bit message that starts at the start index .
