Skip to content

Instantly share code, notes, and snippets.

@kiwiandroiddev
Created February 28, 2020 22:56
Show Gist options
  • Save kiwiandroiddev/234c871a91287f3a5d9ea2c46ba514c0 to your computer and use it in GitHub Desktop.
Save kiwiandroiddev/234c871a91287f3a5d9ea2c46ba514c0 to your computer and use it in GitHub Desktop.
Kotlin function to convert a list of bits (booleans) with big-endian ordering to an integer
/**
* Converts a list of bits (booleans) with big-endian ordering to its corresponding
* integer.
*/
fun List<Boolean>.bigEndianBitsToInt(): Int {
return reversed().foldIndexed(initial = 0) { bitNo: Int, output: Int, bit: Boolean ->
output or (bit.toInt() shl bitNo)
}
}
fun Boolean.toInt() = if (this) 1 else 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment