Skip to content

Instantly share code, notes, and snippets.

@TheOnlyTails
Last active May 6, 2021 10:21
Show Gist options
  • Save TheOnlyTails/fa1f90bfd5a6a2edf17dd02f0b5983da to your computer and use it in GitHub Desktop.
Save TheOnlyTails/fa1f90bfd5a6a2edf17dd02f0b5983da to your computer and use it in GitHub Desktop.
This is a Kotlin implementation for the receiving end of a Hamming Codes (not extended). Given a matrix of bits organized according to a Hamming Code, it will return the location of an error in the matrix or return 0 if there are no errors. Note: the value of bit index 0 is not included in the result, thus you can't store any data in it.
fun hammingSyndrome(bits: List<Int>) =
// gets all the indices of 1 value, and places null in 0 values' indices.
bits.mapIndexed { index, it -> if (it == 1) index else null }
// removes all those null values.
.filterNotNull()
// XORs the indices together to get the index of the error
.reduce { acc, i -> acc xor i }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment