Last active
May 6, 2021 10:21
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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