2

I've been assigned a task where I have to create a SECEncoder in scala that encodes a 32-bit word into a 40-bit word where the least significant digits of the 40-bit word is the 32-bit word.

The SECDecoder should be able to return the same 32-bit word even if there has been at most one bit error.

This far my encoder is able to check for simple parity bits and add them to the word, however I am stuck with my decoder as I'm not able to retrieve the 32-bit word from it correctly.

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
}

So what I'm essentially trying to do is calculating the parity bits through principles similar to Hamming (unfortunately I do not have that much knowledge of it) where the added bits to the 32-bit word are the parity bits.

In the decoder I check the differences in the parity bits of the given word and that of the encoded given word so that I see which parities differ.

Where I am stuck is how do I calculate exactly which bit is differing the whole 32-bit word and then change it? I have come off with some bits when I try to do it, but I never get the exact one.

Am I going in the completely wrong direction or am I just missing something?

Cookie
  • 21
  • 2

0 Answers0