Last active
January 5, 2022 20:13
-
-
Save Pictor13/76ed2fc4449b52addb8157f5ee5297a5 to your computer and use it in GitHub Desktop.
Kotlin - Extension functions
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
// 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