Skip to content

Instantly share code, notes, and snippets.

@Pictor13
Last active January 5, 2022 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pictor13/76ed2fc4449b52addb8157f5ee5297a5 to your computer and use it in GitHub Desktop.
Save Pictor13/76ed2fc4449b52addb8157f5ee5297a5 to your computer and use it in GitHub Desktop.
Kotlin - Extension functions
// Int <--> Boolean
fun Boolean.toInt() = if (this) 1 else 0
fun Int.toBoolean() = if (this == 0) 0 else 1
// Int <--> Binary bit
fun Int.toBit() = this.mod(2).toBoolean().toInt()
fun Int.toBinaryString() = this.toString(2)
fun Int.toPaddedBinaryString() = this.toString(2).padStart(Int.SIZE_BITS, '0')
fun Int.toXaryString(baseX: Int) = this.toString(baseX)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment