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

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

    // Compute wG<sup>T</sup> to check for errors isolated to upper 12 bits.
    let s: u16 = matrix_mul(word, PAR_ALT);
    let n = s.count_ones() as usize;

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

    // Check for cases with one error in lower 12 bits and one or two errors in upper
    // 12 bits [2. p261].
    //
    // For each e<sub>i</sub> = 1 << i, compute (w + e<sub>i</sub>)G<sup>T</sup> =
    // wG<sup>T</sup> + e<sub>i</sub>G<sup>T</sup> = s + a<sub>i</sub>, where
    // a<sub>i</sub> is the i'th row from the bottom of A</sup>T</sup>.
    //
    // Since e<sub>i<sub> isn't used to repair the data bits, we instead just loop
    // over all the words in A<sup>T</sup>.
    for &q in CORE_XPOSE.iter() {
        let syn = s ^ q;
        let n = syn.count_ones() as usize;

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

    // Compute wH<sup>T</sup> to check for errors isolated to lower 12 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 one error in upper 12 bits and 2 errors in lower 12 bits [2,
    // p261].
    //
    // For each e<sub>i</sub>, compute (w + e<sub>i+12</sub>)H<sup>T</sup> =
    // wH<sup>T</sup> + e<sub>i+12</sub>H<sup>T</sup> = s + b<sub>i</sub>, where
    // b<sub>i</sub> is the (i+12)'th row from the bottom of H<sup>T</sup>, which
    // equals the i'th row from the bottom of A.
    for (i, &q) in CORE.iter().enumerate() {
        let syn = s ^ q;

        if syn.count_ones() <= 2 {
            let err = 1 << 11 >> i;
            return Some((data ^ err, 3));
        }
    }

    None
}
/// Try to decode the given 24-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 24-bit word to the nearest codeword, correcting up to 3
errors and detecting 4 errors.


Try to decode the given 24 - 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 >> 24, 0);
    // Strip off parity bits.
    let data = (word >> 12) as u16;
    // Compute wG<sup>T</sup> to check for errors isolated to upper 12 bits.
    let s: u16 = matrix_mul(word, PAR_ALT);
    let n = s.count_ones() as usize;
    if n <= 3 {
        return Some((data ^ s, n));
    }
    // Check for cases with one error in lower 12 bits and one or two errors in upper
    // 12 bits [2. p261].
    //
    // For each e<sub>i</sub> = 1 << i, compute (w + e<sub>i</sub>)G<sup>T</sup> =
    // wG<sup>T</sup> + e<sub>i</sub>G<sup>T</sup> = s + a<sub>i</sub>, where
    // a<sub>i</sub> is the i'th row from the bottom of A</sup>T</sup>.
    //
    // Since e<sub>i<sub> isn't used to repair the data bits, we instead just loop
    // over all the words in A<sup>T</sup>.
    for &q in CORE_XPOSE.iter() {
        let syn = s ^ q;
        let n = syn.count_ones() as usize;
        if n <= 2 {
            return Some((data ^ syn, n + 1));
        }
    }
    // Compute wH<sup>T</sup> to check for errors isolated to lower 12 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 one error in upper 12 bits and 2 errors in lower 12 bits [2,
    // p261].
    //
    // For each e<sub>i</sub>, compute (w + e<sub>i+12</sub>)H<sup>T</sup> =
    // wH<sup>T</sup> + e<sub>i+12</sub>H<sup>T</sup> = s + b<sub>i</sub>, where
    // b<sub>i</sub> is the (i+12)'th row from the bottom of H<sup>T</sup>, which
    // equals the i'th row from the bottom of A.
    for (i, &q) in CORE.iter().enumerate() {
        let syn = s ^ q;
        if syn.count_ones() <= 2 {
            let err = 1 << 11 >> i;
            return Some((data ^ err, 3));
        }
    }
    None
}
pub fn decode ( word : u32 ) -> Option < ( u16 , usize ) > { assert_eq ! ( word 24 0 ) ; // Strip off parity bits. let data = ( word >> 12 ) as u16 ; // Compute wG<sup>T</sup> to check for errors isolated to upper 12 bits. let s : u16 = matrix_mul ( word , PAR_ALT ) ; let n = s . count_ones ( ) as usize ; if n <= 3 { return Some ( ( data ^ s , n ) ) ; } // Check for cases with one error in lower 12 bits and one or two errors in upper // 12 bits [2. p261]. // // For each e<sub>i</sub> = 1 << i, compute (w + e<sub>i</sub>)G<sup>T</sup> = // wG<sup>T</sup> + e<sub>i</sub>G<sup>T</sup> = s + a<sub>i</sub>, where // a<sub>i</sub> is the i'th row from the bottom of A</sup>T</sup>. // // Since e<sub>i<sub> isn't used to repair the data bits, we instead just loop // over all the words in A<sup>T</sup>. for & q in CORE_XPOSE . iter ( ) { let syn = s ^ q ; let n = syn . count_ones ( ) as usize ; if n <= 2 { return Some ( ( data ^ syn , n + 1 ) ) ; } } // Compute wH<sup>T</sup> to check for errors isolated to lower 12 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 one error in upper 12 bits and 2 errors in lower 12 bits [2, // p261]. // // For each e<sub>i</sub>, compute (w + e<sub>i+12</sub>)H<sup>T</sup> = // wH<sup>T</sup> + e<sub>i+12</sub>H<sup>T</sup> = s + b<sub>i</sub>, where // b<sub>i</sub> is the (i+12)'th row from the bottom of H<sup>T</sup>, which // equals the i'th row from the bottom of A. for ( i , & q ) in CORE . iter ( ) . enumerate ( ) { let syn = s ^ q ; if syn . count_ones ( ) <= 2 { let err = 1 << 11 >> i ; return Some ( ( data ^ err , 3 ) ) ; } } None }
Try to decode the given 24-bit word to the nearest codeword, correcting up to 3
errors and detecting 4 errors.
Try to decode the given 24 - bit word to the nearest codeword correcting up to 3 errors and detecting 4 errors .
word
u32
word
u32