find and correct single bit errors in 16-bit hamming codes

This commit is contained in:
2024-07-06 20:37:07 -04:00
parent c494a25587
commit ed405e366e
2 changed files with 38 additions and 4 deletions

View File

@@ -26,7 +26,15 @@ License along with this program. If not, see
module Hamming (Hamming (..)) where
import Data.Bits (Bits, complement, shiftR, (.&.), (.|.))
import Data.Bits
( Bits
, complement
, shiftL
, shiftR
, xor
, (.&.)
, (.|.)
)
import Data.Word (Word16)
-- | An implementation of a Hamming code
@@ -55,7 +63,19 @@ instance Hamming Word16 where
, 0xffff
]
correctErrors = undefined
correctErrors c
| isValid c = Just c
| otherwise = let
errorFlags = map (oddParity . (c .&.))
[0xaaaa, 0xcccc, 0xf0f0, 0xff00]
flipAddr = foldl
( \a (mask, err) -> if err
then let
in a .|. mask
else a
) 0 $ zip [1, 2, 4, 8] errorFlags
flipBit = 1 `shiftL` flipAddr
in Just $ c `xor` flipBit
setCheckBit :: (Num a, Bits a) => a -> (a, a) -> a
setCheckBit a (pBit, chkMask) =