public static int checkAndCorrect(BinaryMessage frame,int startIndex){
  int syndrome=getSyndrome(frame,startIndex);
  if (syndrome == 0) {
    return 0;
  }
  BinaryMessage copy=frame.getSubMessage(startIndex,startIndex + 23);
  int index=-1;
  int syndromeWeight=MAX_CORRECTABLE_ERRORS;
  int errors=0;
  while (index < 23) {
    if (index != -1) {
      if (index > 0) {
        copy.flip(index - 1);
      }
      copy.flip(index);
      syndromeWeight=MAX_CORRECTABLE_ERRORS - 1;
    }
    syndrome=getSyndrome(copy,0);
    if (syndrome > 0) {
      for (int i=0; i < 23; i++) {
        errors=Integer.bitCount(syndrome);
        if (errors <= syndromeWeight) {
          copy.xor(12,11,syndrome);
          copy.rotateRight(i,0,22);
          if (index >= 0) {
            errors++;
          }
          int corrected=copy.getInt(0,22);
          int original=frame.getInt(startIndex,startIndex + 22);
          int errorCount=Integer.bitCount(original ^ corrected);
          if (errorCount <= 3) {
            frame.load(startIndex,23,corrected);
          }
          return errorCount;
        }
 else {
          copy.rotateLeft(0,22);
          syndrome=getSyndrome(copy,0);
        }
      }
      index++;
    }
  }
  return 4;
}

public static int checkAndCorrect ( BinaryMessage frame , int startIndex ) { int syndrome = getSyndrome ( frame , startIndex ) ; if ( syndrome == 0 ) { return 0 ; } BinaryMessage copy = frame . getSubMessage ( startIndex , startIndex + 23 ) ; int index = - 1 ; int syndromeWeight = MAX_CORRECTABLE_ERRORS ; int errors = 0 ; while ( index < 23 ) { if ( index != - 1 ) { if ( index > 0 ) { copy . flip ( index - 1 ) ; } copy . flip ( index ) ; syndromeWeight = MAX_CORRECTABLE_ERRORS - 1 ; } syndrome = getSyndrome ( copy , 0 ) ; if ( syndrome > 0 ) { for ( int i = 0 ; i < 23 ; i ++ ) { errors = Integer . bitCount ( syndrome ) ; if ( errors <= syndromeWeight ) { copy . xor ( 12 , 11 , syndrome ) ; copy . rotateRight ( i , 0 , 22 ) ; if ( index >= 0 ) { errors ++ ; } int corrected = copy . getInt ( 0 , 22 ) ; int original = frame . getInt ( startIndex , startIndex + 22 ) ; int errorCount = Integer . bitCount ( original ^ corrected ) ; if ( errorCount <= 3 ) { frame . load ( startIndex , 23 , corrected ) ; } return errorCount ; } else { copy . rotateLeft ( 0 , 22 ) ; syndrome = getSyndrome ( copy , 0 ) ; } } index ++ ; } } return 4 ; }
implements golay ( 23 , 12 , 7 ) error detection and correction . returns the number of detected errors . if the error count is less than or equal to the max correctable errors ( 3 ) , then the error bits are corrected . otherwise the message is left intact and an error count greater than 3 is returned .
