在scala中创建简单的SECEncoder和SECDecoder

在scala中创建简单的SECEncoder和SECDecoder,scala,encoder,decoder,hamming-code,Scala,Encoder,Decoder,Hamming Code,我被分配了一项任务,我必须在scala中创建一个SECEncoder,将32位字编码为40位字,其中40位字的最低有效位是32位字 即使出现最多一位错误,SECDecoder也应该能够返回相同的32位字 到目前为止,我的编码器能够检查简单的奇偶校验位并将它们添加到字中,但是我仍然使用我的解码器,因为我无法正确地从中检索32位字 val p = Vector(0x55555555L, 0x33333333L, 0x471C71C7L, 0x0F0F0F0FL, 0x41F07C1FL, 0x3F0

我被分配了一项任务,我必须在scala中创建一个SECEncoder,将32位字编码为40位字,其中40位字的最低有效位是32位字

即使出现最多一位错误,SECDecoder也应该能够返回相同的32位字

到目前为止,我的编码器能够检查简单的奇偶校验位并将它们添加到字中,但是我仍然使用我的解码器,因为我无法正确地从中检索32位字

val p = Vector(0x55555555L, 0x33333333L, 0x471C71C7L, 0x0F0F0F0FL, 0x41F07C1FL, 0x3F03F03FL, 0x701FC07FL, 0x00FF00FFL)
def ham(j: Int, m: Int) = ((0 to 31).map(i => (((m & p(j-1)) >> i)&1)).sum) % 2

def SECEncode(d: Int): Long = {

   val ps = Vector(ham(1, d), ham(2, d), ham(3, d), ham(4, d), ham(5, d), ham(6, d), ham(7, d), ham(8, d))
         // calculates the value of the parity bits

   var dN = d.toLong

   for (i <- 0 to 7) {
     dN += (ps(i) << (31.toLong + i.toLong))
   }

   dN
}


def SECDecode(s: Long): Int = {
    val d  = (s & Int.MaxValue).toInt // takes the 32 least significant bits of _s_
    val check = (s >>> 31) ^ (SECEncode(d) >>> 31) // checks which bits in the encoding differ

    import scala.collection.mutable.Buffer
    val buf1 = Buffer[Long]()
    val buf2 = Buffer[Long]()

    for (i <- 0 to 7) {
      if (((check >>> i)&1) == 1) buf1 += p(i) else buf2 += p(i)
    }

    val b = buf1.reduce(_ ^ _) | buf2.reduce(_ | _)

    (((b ^ Int.MaxValue)) ^ d).toInt
}
val p=Vector(0x5555555555L、0x33333333L、0x471C71C7L、0x0F0F0FL、0x41F07C1FL、0x3F03F03FL、0x701FC07FL、0x00FF00FFL)
def ham(j:Int,m:Int)=(0到31).map(i=>((m&p(j-1))>>i)和1)).sum)%2
def SECEncode(d:Int):长={
val ps=向量(火腿(1,d),火腿(2,d),火腿(3,d),火腿(4,d),火腿(5,d),火腿(6,d),火腿(7,d),火腿(8,d))
//计算奇偶校验位的值
var dN=d.toLong
对于(i>31)^(SECEncode(d)>>>31)//检查编码中的哪些位不同
导入scala.collection.mutable.Buffer
val buf1=缓冲区[Long]()
val buf2=缓冲区[长]()
对于(i>>i)&1)==1)buf1+=p(i),否则buf2+=p(i)
}
val b=buf1.reduce(^)| buf2.reduce(^ |)
((b^Int.MaxValue))^d).toInt
}
所以我基本上是想通过类似于Hamming的原理来计算奇偶校验位(不幸的是我对它没有太多的了解),其中32位字的附加位就是奇偶校验位

在解码器中,我检查给定字的奇偶校验位与编码给定字的奇偶校验位之间的差异,以便查看哪些奇偶校验位不同

我陷入困境的是,我如何准确地计算出哪个位与整个32位字不同,然后对其进行更改?我试着做这件事的时候,有一些零碎的东西掉了下来,但我从来没有得到确切的一个

我是走错方向了,还是我错过了什么