Using scala 3, my solution did not pass the test: "* the SEC encoding and decoding algorithm should return the correct value - For d = 0x228F1D3C, s = 0x0086C2C88C, k = 10, e = 0x0086C2CC8C
9379238 was not equal to 579804476".

Code:
/*
 * Assignment:  Single bit-error correction
 *
 * Motivation:
 * While individual computers are fairly reliable devices, hardware 
 * failures become a regular occurrence as the system size is scaled up 
 * from an individual computer to, say, a warehouse-scale compute cluster.
 * When designing such systems, careful attention needs to be paid to
 * __fault tolerance__. That is, the system must be engineered to 
 * automatically and transparently recover from small hardware failures. 
 * Individual bit errors in DRAM (dynamic random access memory) modules 
 * are among such failures, as you can discover from the following 
 * large-scale field study available at Google Research:
 * 
 * http://research.google.com/pubs/pub35162.html
 *
 * Description:
 * This assignment asks you to study the principles of error-correcting
 * codes, after which you get to design your own error-correction scheme
 * to protect data stored in a (hypothetical) DRAM module from bit-errors.
 * (We only give you the specification that your design needs to meet.)
 *
 * Hint: 
 * To successfully solve this assignment, you may want to study, for 
 * example, the Hamming codes.
 *
 * http://en.wikipedia.org/wiki/Hamming_code
 *
 */

package object errorCorrect:

  /*
   * Task: Single bit-error correction
   *
   * Suppose you have available a memory module whose storage elements are 
   * 40-bit words. Each storage element is subject to bit errors, that is,
   * each bit in the 40-bit word may change its state from 0 to 1 or vice 
   * versa in a manner that we as system designers cannot control. 
   *
   * Using the 40-bit storage elements, we want implement a storage
   * scheme for 32-bit data words that is able to always recover the 
   * original data word if __at most one bit error__ has occurred during 
   * storage. That is, any individual bit in the storage element may 
   * experience a bit error, and we must still be able to recover the 
   * original data word from the bits stored in the storage element.
   * In particular, this requires that we __encode__ the 32-bit word
   * into a 40-bit word that gets saved into the storage element,
   * and __decode__ the 40-bit word loaded from the storage element 
   * back into the 32-bit word. Decoding must always succeed if at most
   * one bit error has occurred during storage. (The result of decoding
   * may be arbitrary if there has been more than one bit error.) 
   *
   * This task asks you to design encoding and decoding functions
   * that implement the previous specification.
   *
   */

  /** Returns, in the least significant 40 bits of the return value,
   *  the 40-bit encoded form of the 32-bit data word given as parameter. */

  def SECEncode(d: Int): Long =
    val data: Long = (d & 0xFFFFFFFFL)
    var encoded: Long = 0L
    
    // Place data elements to the right position within the 40-bit encoded storage
    for i <- 0 to 31 do
      val bit = (data >> i) & 1
      val j = i + (i / 3) + 1
      encoded = encoded | ((bit << j) & 0xFFFFFFFFL)

    // Calculate parity bits
    for i <- 0 to 5 do
      val parityBit = (1 << i)
      val parityBits = (encoded >> 1) & ~((1L << i) - 1L)
      var parity = 0L
      for j <- i + 1 to 30 by (2 * parityBit) do
        parity ^= (parityBits >> j) & ((1L << i) - 1L)
      val parityBitVal = java.lang.Long.bitCount(parity) & 1
      encoded = (encoded & (~(parityBit << 1))) | (parityBitVal << i)

    encoded
  end SECEncode

  def SECDecode(s: Long): Int =
    var paritySum = 0
    
    // Check parity bits to detect error position
    for i <- 0 to 5 do
      val parityBit = (1 << i)
      val parityBits = (s >> 1) & ~((1L << i) - 1L)
      var parity = (s >> i) & 1      
      for j <- i + 1 to 30 by (2 * parityBit) do
        parity ^= (parityBits >> j) & ((1L << i) - 1L)
      paritySum = paritySum | (parity << i).toInt

    // If there is a bit error, correct it
    val repaired = if paritySum != 0 then s ^ (1L << paritySum) else s

    var decoded: Int = 0
    for i <- 0 to 31 do
      val j = i + (i / 3) + 1
      decoded = decoded | ((((repaired >> j) & 1L) << i) & 0xFFFFFFFFL).toInt

    decoded
  end SECDecode