pub fn decode(word: u32) -> Option<(u16, usize)> {
    assert_eq!(word >> 23, 0);

    // Strip off parity bits.
    let data = (word >> 11) as u16;

    // Check for 1 to 3 errors isolated in the parity bits.
    let s: u16 = matrix_mul(word, PAR);
    let n = s.count_ones() as usize;

    if n <= 3 {
        return Some((data, n));
    }

    // Check for cases with 1 error in the data bits and 0 to 2 errors in the parity bits.
    for (i, &syn) in SYN.iter().enumerate() {
        let n = (s ^ syn).count_ones() as usize;

        if n <= 2 {
            return Some((data ^ 1 << i, n + 1));
        }
    }

    // Check for 2 or 3 errors isolated to the data bits (except data MSB).
    let s: u16 = matrix_mul(rotate_11(word), PAR);
    let n = s.count_ones() as usize;

    if n <= 3 {
        return Some((data ^ s, n));
    }

    // Check for cases with 2 or 3 errors in the data bits (one being the data MSB) and
    // possibly 1 error in the parity bits or 2 errors in the data bits (exluding data
    // MSB) and 1 error in the parity bits.
    for (i, &syn) in SYN.iter().enumerate() {
        let r = s ^ syn;
        let n = r.count_ones() as usize;

        if n <= 2 {
            // The 0th syndrome corresponds to the data MSB (which now lies in the upper
            // 12 bits of the rotated word), so it must be flipped, but the following
            // syndromes correspond to parity bits, which don't need to be flipped.
            return if i == 0 {
                Some((data ^ r ^ 1 << 11, n + 1))
            } else {
                Some((data ^ r, 3))
            };
        }
    }

    None
}
/// Try to decode the given 23-bit word to the nearest codeword, correcting up to 3
/// errors and detecting 4 errors.
///
/// If decoding was successful, return `Some((data, err))`, where `data` is the 12
/// data bits and `err` is the number of corrected bits. Otherwise, return `None` to
/// indicate an unrecoverable error.
Try to decode the given 23-bit word to the nearest codeword, correcting up to 3
errors and detecting 4 errors.


Try to decode the given 23 - bit word to the nearest codeword correcting up to 3 errors and detecting 4 errors .
pub fn decode(word: u32) -> Option<(u16, usize)> {
    assert_eq!(word >> 23, 0);
    // Strip off parity bits.
    let data = (word >> 11) as u16;
    // Check for 1 to 3 errors isolated in the parity bits.
    let s: u16 = matrix_mul(word, PAR);
    let n = s.count_ones() as usize;
    if n <= 3 {
        return Some((data, n));
    }
    // Check for cases with 1 error in the data bits and 0 to 2 errors in the parity bits.
    for (i, &syn) in SYN.iter().enumerate() {
        let n = (s ^ syn).count_ones() as usize;
        if n <= 2 {
            return Some((data ^ 1 << i, n + 1));
        }
    }
    // Check for 2 or 3 errors isolated to the data bits (except data MSB).
    let s: u16 = matrix_mul(rotate_11(word), PAR);
    let n = s.count_ones() as usize;
    if n <= 3 {
        return Some((data ^ s, n));
    }
    // Check for cases with 2 or 3 errors in the data bits (one being the data MSB) and
    // possibly 1 error in the parity bits or 2 errors in the data bits (exluding data
    // MSB) and 1 error in the parity bits.
    for (i, &syn) in SYN.iter().enumerate() {
        let r = s ^ syn;
        let n = r.count_ones() as usize;
        if n <= 2 {
            // The 0th syndrome corresponds to the data MSB (which now lies in the upper
            // 12 bits of the rotated word), so it must be flipped, but the following
            // syndromes correspond to parity bits, which don't need to be flipped.
            return if i == 0 {
                Some((data ^ r ^ 1 << 11, n + 1))
            } else {
                Some((data ^ r, 3))
            };
        }
    }
    None
}
pub fn decode ( word : u32 ) -> Option < ( u16 , usize ) > { assert_eq ! ( word 23 0 ) ; // Strip off parity bits. let data = ( word >> 11 ) as u16 ; // Check for 1 to 3 errors isolated in the parity bits. let s : u16 = matrix_mul ( word , PAR ) ; let n = s . count_ones ( ) as usize ; if n <= 3 { return Some ( ( data , n ) ) ; } // Check for cases with 1 error in the data bits and 0 to 2 errors in the parity bits. for ( i , & syn ) in SYN . iter ( ) . enumerate ( ) { let n = ( s ^ syn ) . count_ones ( ) as usize ; if n <= 2 { return Some ( ( data ^ 1 << i , n + 1 ) ) ; } } // Check for 2 or 3 errors isolated to the data bits (except data MSB). let s : u16 = matrix_mul ( rotate_11 ( word ) , PAR ) ; let n = s . count_ones ( ) as usize ; if n <= 3 { return Some ( ( data ^ s , n ) ) ; } // Check for cases with 2 or 3 errors in the data bits (one being the data MSB) and // possibly 1 error in the parity bits or 2 errors in the data bits (exluding data // MSB) and 1 error in the parity bits. for ( i , & syn ) in SYN . iter ( ) . enumerate ( ) { let r = s ^ syn ; let n = r . count_ones ( ) as usize ; if n <= 2 { // The 0th syndrome corresponds to the data MSB (which now lies in the upper // 12 bits of the rotated word), so it must be flipped, but the following // syndromes correspond to parity bits, which don't need to be flipped. return if i == 0 { Some ( ( data ^ r ^ 1 << 11 , n + 1 ) ) } else { Some ( ( data ^ r , 3 ) ) } ; } } None }
Try to decode the given 23-bit word to the nearest codeword, correcting up to 3
errors and detecting 4 errors.
Try to decode the given 23 - bit word to the nearest codeword correcting up to 3 errors and detecting 4 errors .
word
u32
word
u32