Skip to content

Instantly share code, notes, and snippets.

@ElianFabian
Created May 16, 2024 00:14
Show Gist options
  • Select an option

  • Save ElianFabian/6f888840f15a5e1113ba7b6f78b77860 to your computer and use it in GitHub Desktop.

Select an option

Save ElianFabian/6f888840f15a5e1113ba7b6f78b77860 to your computer and use it in GitHub Desktop.
import kotlin.experimental.and
import kotlin.experimental.or
import kotlin.experimental.xor
fun UByte.toFixedBinaryString(): String {
return toString(2).padStart(UByte.SIZE_BITS, '0')
}
fun UShort.toFixedBinaryString(byteSeparator: String = ""): String {
val binaryString = toString(2).padStart(UShort.SIZE_BITS, '0').also { binaryString ->
if (byteSeparator == "") return binaryString
}
var separator = ""
return buildString {
binaryString.forEachIndexed { index, char ->
if (index % 8 == 0) {
append(separator)
separator = byteSeparator
}
append(char)
}
}
}
fun UInt.toFixedBinaryString(byteSeparator: String = ""): String {
val binaryString = toString(2).padStart(UInt.SIZE_BITS, '0').also { binaryString ->
if (byteSeparator == "") return binaryString
}
var separator = ""
return buildString {
binaryString.forEachIndexed { index, char ->
if (index % 8 == 0) {
append(separator)
separator = byteSeparator
}
append(char)
}
}
}
fun ULong.toFixedBinaryString(byteSeparator: String = ""): String {
val binaryString = toString(2).padStart(ULong.SIZE_BITS, '0').also { binaryString ->
if (byteSeparator == "") return binaryString
}
var separator = ""
return buildString {
binaryString.forEachIndexed { index, char ->
if (index % 8 == 0) {
append(separator)
separator = byteSeparator
}
append(char)
}
}
}
fun Byte.toFixedBinaryString() = this.toUByte().toFixedBinaryString()
fun Short.toFixedBinaryString(byteSeparator: String = "") = this.toUShort().toFixedBinaryString(byteSeparator)
fun Int.toFixedBinaryString(byteSeparator: String = "") = this.toUInt().toFixedBinaryString(byteSeparator)
fun Long.toFixedBinaryString(byteSeparator: String = "") = this.toULong().toFixedBinaryString(byteSeparator)
fun Float.toFixedBinaryString(byteSeparator: String = "") = this.toBits().toFixedBinaryString(byteSeparator)
fun Double.toFixedBinaryString(byteSeparator: String = "") = this.toBits().toFixedBinaryString(byteSeparator)
fun Byte.asBitSequence(): Sequence<Int> {
val value = this
return sequence {
for (i in 0 until Byte.SIZE_BITS) {
val bit = (value.toInt() ushr i) and 1
yield(bit)
}
}
}
fun Short.asBitSequence(): Sequence<Int> {
val value = this
return sequence {
for (i in 0 until Short.SIZE_BITS) {
val bit = (value.toInt() ushr i) and 1
yield(bit)
}
}
}
fun Int.asBitSequence(): Sequence<Int> {
val value = this
return sequence {
for (i in 0 until Int.SIZE_BITS) {
val bit = (value ushr i) and 1
yield(bit)
}
}
}
fun Long.asBitSequence(): Sequence<Int> {
val value = this
return sequence {
for (i in 0 until Long.SIZE_BITS) {
val bit = ((value ushr i) and 1).toInt()
yield(bit)
}
}
}
fun Char.asBitSequence(): Sequence<Int> {
val value = code
return sequence {
for (i in 0 until Char.SIZE_BITS) {
val bit = (value ushr i) and 1
yield(bit)
}
}
}
fun UByte.asBitSequence(): Sequence<Int> {
val value = this
return sequence {
for (i in 0 until UByte.SIZE_BITS) {
val bit = (value.toInt() ushr i) and 1
yield(bit)
}
}
}
fun UShort.asBitSequence(): Sequence<Int> {
val value = this
return sequence {
for (i in 0 until UShort.SIZE_BITS) {
val bit = (value.toInt() shl i) and 1
yield(bit)
}
}
}
fun UInt.asBitSequence(): Sequence<Int> {
val value = this
return sequence {
for (i in 0 until UInt.SIZE_BITS) {
val bit = ((value shr i) and 1u).toInt()
yield(bit)
}
}
}
fun ULong.asBitSequence(): Sequence<Int> {
val value = this
return sequence {
for (i in 0 until ULong.SIZE_BITS) {
val bit = ((value shr i) and 1u).toInt()
yield(bit)
}
}
}
inline fun Byte.forEachBit(action: (bit: Int) -> Unit) {
for (i in 0 until Byte.SIZE_BITS) {
val bit = (this.toInt() ushr i) and 1
action(bit)
}
}
inline fun Byte.forEachBitIndexed(action: (index: Int, bit: Int) -> Unit) {
for (i in 0 until Byte.SIZE_BITS) {
val bit = (this.toInt() ushr i) and 1
action(i, bit)
}
}
inline fun Short.forEachBit(action: (bit: Int) -> Unit) {
for (i in 0 until Short.SIZE_BITS) {
val bit = (this.toInt() ushr i) and 1
action(bit)
}
}
inline fun Short.forEachBitIndexed(action: (index: Int, bit: Int) -> Unit) {
for (i in 0 until Short.SIZE_BITS) {
val bit = (this.toInt() ushr i) and 1
action(i, bit)
}
}
inline fun Int.forEachBit(action: (bit: Int) -> Unit) {
for (i in 0 until Int.SIZE_BITS) {
val bit = (this ushr i) and 1
action(bit)
}
}
inline fun Int.forEachBitIndexed(action: (index: Int, bit: Int) -> Unit) {
for (i in 0 until Int.SIZE_BITS) {
val bit = (this ushr i) and 1
action(i, bit)
}
}
inline fun Long.forEachBit(action: (bit: Int) -> Unit) {
for (i in 0 until Long.SIZE_BITS) {
val bit = ((this ushr i) and 1).toInt()
action(bit)
}
}
inline fun Long.forEachBitIndexed(action: (index: Int, bit: Int) -> Unit) {
for (i in 0 until Long.SIZE_BITS) {
val bit = ((this ushr i) and 1).toInt()
action(i, bit)
}
}
inline fun Char.forEachBit(action: (bit: Int) -> Unit) {
val value = code
for (i in 0 until Char.SIZE_BITS) {
val bit = (value ushr i) and 1
action(bit)
}
}
inline fun Char.forEachBitIndexed(action: (index: Int, bit: Int) -> Unit) {
val value = code
for (i in 0 until Char.SIZE_BITS) {
val bit = (value ushr i) and 1
action(i, bit)
}
}
inline fun UByte.forEachBit(action: (bit: Int) -> Unit) {
for (i in 0 until UByte.SIZE_BITS) {
val bit = (this.toInt() ushr i) and 1
action(bit)
}
}
inline fun UByte.forEachBitIndexed(action: (index: Int, bit: Int) -> Unit) {
for (i in 0 until UByte.SIZE_BITS) {
val bit = (this.toInt() ushr i) and 1
action(i, bit)
}
}
inline fun UShort.forEachBit(action: (bit: Int) -> Unit) {
for (i in 0 until UShort.SIZE_BITS) {
val bit = (this.toInt() ushr i) and 1
action(bit)
}
}
inline fun UShort.forEachBitIndexed(action: (index: Int, bit: Int) -> Unit) {
for (i in 0 until UShort.SIZE_BITS) {
val bit = (this.toInt() ushr i) and 1
action(i, bit)
}
}
inline fun UInt.forEachBit(action: (bit: Int) -> Unit) {
for (i in 0 until UInt.SIZE_BITS) {
val bit = ((this shr i) and 1u).toInt()
action(bit)
}
}
inline fun UInt.forEachBitIndexed(action: (index: Int, bit: Int) -> Unit) {
for (i in 0 until UInt.SIZE_BITS) {
val bit = ((this shr i) and 1u).toInt()
action(i, bit)
}
}
inline fun ULong.forEachBit(action: (bit: Int) -> Unit) {
for (i in 0 until ULong.SIZE_BITS) {
val bit = ((this shr i) and 1u).toInt()
action(bit)
}
}
inline fun ULong.forEachBitIndexed(action: (index: Int, bit: Int) -> Unit) {
for (i in 0 until ULong.SIZE_BITS) {
val bit = ((this shr i) and 1u).toInt()
action(i, bit)
}
}
inline fun Byte.inlineForEachBit(action: (bit: Int) -> Unit) {
val value = this.toInt()
action((value ushr 0) and 1)
action((value ushr 1) and 1)
action((value ushr 2) and 1)
action((value ushr 3) and 1)
action((value ushr 4) and 1)
action((value ushr 5) and 1)
action((value ushr 6) and 1)
action((value ushr 7) and 1)
}
inline fun Short.inlineForEachBit(action: (bit: Int) -> Unit) {
val value = this.toInt()
action((value ushr 0) and 1)
action((value ushr 1) and 1)
action((value ushr 2) and 1)
action((value ushr 3) and 1)
action((value ushr 4) and 1)
action((value ushr 5) and 1)
action((value ushr 6) and 1)
action((value ushr 7) and 1)
action((value ushr 8) and 1)
action((value ushr 9) and 1)
action((value ushr 10) and 1)
action((value ushr 11) and 1)
action((value ushr 12) and 1)
action((value ushr 13) and 1)
action((value ushr 14) and 1)
action((value ushr 15) and 1)
}
inline fun Int.inlineForEachBit(action: (bit: Int) -> Unit) {
val value = this
action((value ushr 0) and 1)
action((value ushr 1) and 1)
action((value ushr 2) and 1)
action((value ushr 3) and 1)
action((value ushr 4) and 1)
action((value ushr 5) and 1)
action((value ushr 6) and 1)
action((value ushr 7) and 1)
action((value ushr 8) and 1)
action((value ushr 9) and 1)
action((value ushr 10) and 1)
action((value ushr 11) and 1)
action((value ushr 12) and 1)
action((value ushr 13) and 1)
action((value ushr 14) and 1)
action((value ushr 15) and 1)
action((value ushr 16) and 1)
action((value ushr 17) and 1)
action((value ushr 18) and 1)
action((value ushr 19) and 1)
action((value ushr 20) and 1)
action((value ushr 21) and 1)
action((value ushr 22) and 1)
action((value ushr 23) and 1)
action((value ushr 24) and 1)
action((value ushr 25) and 1)
action((value ushr 26) and 1)
action((value ushr 27) and 1)
action((value ushr 28) and 1)
action((value ushr 29) and 1)
action((value ushr 30) and 1)
action((value ushr 31) and 1)
}
inline fun Long.inlineForEachBit(action: (bit: Int) -> Unit) {
val value = this
action(((value ushr 0) and 1).toInt())
action(((value ushr 1) and 1).toInt())
action(((value ushr 2) and 1).toInt())
action(((value ushr 3) and 1).toInt())
action(((value ushr 4) and 1).toInt())
action(((value ushr 5) and 1).toInt())
action(((value ushr 6) and 1).toInt())
action(((value ushr 7) and 1).toInt())
action(((value ushr 8) and 1).toInt())
action(((value ushr 9) and 1).toInt())
action(((value ushr 10) and 1).toInt())
action(((value ushr 11) and 1).toInt())
action(((value ushr 12) and 1).toInt())
action(((value ushr 13) and 1).toInt())
action(((value ushr 14) and 1).toInt())
action(((value ushr 15) and 1).toInt())
action(((value ushr 16) and 1).toInt())
action(((value ushr 17) and 1).toInt())
action(((value ushr 18) and 1).toInt())
action(((value ushr 19) and 1).toInt())
action(((value ushr 20) and 1).toInt())
action(((value ushr 21) and 1).toInt())
action(((value ushr 22) and 1).toInt())
action(((value ushr 23) and 1).toInt())
action(((value ushr 24) and 1).toInt())
action(((value ushr 25) and 1).toInt())
action(((value ushr 26) and 1).toInt())
action(((value ushr 27) and 1).toInt())
action(((value ushr 28) and 1).toInt())
action(((value ushr 29) and 1).toInt())
action(((value ushr 30) and 1).toInt())
action(((value ushr 31) and 1).toInt())
action(((value ushr 32) and 1).toInt())
action(((value ushr 33) and 1).toInt())
action(((value ushr 34) and 1).toInt())
action(((value ushr 35) and 1).toInt())
action(((value ushr 36) and 1).toInt())
action(((value ushr 37) and 1).toInt())
action(((value ushr 38) and 1).toInt())
action(((value ushr 39) and 1).toInt())
action(((value ushr 40) and 1).toInt())
action(((value ushr 41) and 1).toInt())
action(((value ushr 42) and 1).toInt())
action(((value ushr 43) and 1).toInt())
action(((value ushr 44) and 1).toInt())
action(((value ushr 45) and 1).toInt())
action(((value ushr 46) and 1).toInt())
action(((value ushr 47) and 1).toInt())
action(((value ushr 48) and 1).toInt())
action(((value ushr 49) and 1).toInt())
action(((value ushr 50) and 1).toInt())
action(((value ushr 51) and 1).toInt())
action(((value ushr 52) and 1).toInt())
action(((value ushr 53) and 1).toInt())
action(((value ushr 54) and 1).toInt())
action(((value ushr 55) and 1).toInt())
action(((value ushr 56) and 1).toInt())
action(((value ushr 57) and 1).toInt())
action(((value ushr 58) and 1).toInt())
action(((value ushr 59) and 1).toInt())
action(((value ushr 60) and 1).toInt())
action(((value ushr 61) and 1).toInt())
action(((value ushr 62) and 1).toInt())
action(((value ushr 63) and 1).toInt())
}
inline fun Char.inlineForEachBit(action: (bit: Int) -> Unit) {
val value = code
action((value ushr 0) and 1)
action((value ushr 1) and 1)
action((value ushr 2) and 1)
action((value ushr 3) and 1)
action((value ushr 4) and 1)
action((value ushr 5) and 1)
action((value ushr 6) and 1)
action((value ushr 7) and 1)
action((value ushr 8) and 1)
action((value ushr 9) and 1)
action((value ushr 10) and 1)
action((value ushr 11) and 1)
action((value ushr 12) and 1)
action((value ushr 13) and 1)
action((value ushr 14) and 1)
action((value ushr 15) and 1)
}
inline fun UByte.inlineForEachBit(action: (bit: Int) -> Unit) {
val value = this.toInt()
action((value ushr 0) and 1)
action((value ushr 1) and 1)
action((value ushr 2) and 1)
action((value ushr 3) and 1)
action((value ushr 4) and 1)
action((value ushr 5) and 1)
action((value ushr 6) and 1)
action((value ushr 7) and 1)
}
inline fun UShort.inlineForEachBit(action: (bit: Int) -> Unit) {
val value = this.toInt()
action((value ushr 0) and 1)
action((value ushr 1) and 1)
action((value ushr 2) and 1)
action((value ushr 3) and 1)
action((value ushr 4) and 1)
action((value ushr 5) and 1)
action((value ushr 6) and 1)
action((value ushr 7) and 1)
action((value ushr 8) and 1)
action((value ushr 9) and 1)
action((value ushr 10) and 1)
action((value ushr 11) and 1)
action((value ushr 12) and 1)
action((value ushr 13) and 1)
action((value ushr 14) and 1)
action((value ushr 15) and 1)
}
inline fun UInt.inlineForEachBit(action: (bit: Int) -> Unit) {
val value = this
action(((value shr 0) and 1u).toInt())
action(((value shr 1) and 1u).toInt())
action(((value shr 2) and 1u).toInt())
action(((value shr 3) and 1u).toInt())
action(((value shr 4) and 1u).toInt())
action(((value shr 5) and 1u).toInt())
action(((value shr 6) and 1u).toInt())
action(((value shr 7) and 1u).toInt())
action(((value shr 8) and 1u).toInt())
action(((value shr 9) and 1u).toInt())
action(((value shr 10) and 1u).toInt())
action(((value shr 11) and 1u).toInt())
action(((value shr 12) and 1u).toInt())
action(((value shr 13) and 1u).toInt())
action(((value shr 14) and 1u).toInt())
action(((value shr 15) and 1u).toInt())
action(((value shr 16) and 1u).toInt())
action(((value shr 17) and 1u).toInt())
action(((value shr 18) and 1u).toInt())
action(((value shr 19) and 1u).toInt())
action(((value shr 20) and 1u).toInt())
action(((value shr 21) and 1u).toInt())
action(((value shr 22) and 1u).toInt())
action(((value shr 23) and 1u).toInt())
action(((value shr 24) and 1u).toInt())
action(((value shr 25) and 1u).toInt())
action(((value shr 26) and 1u).toInt())
action(((value shr 27) and 1u).toInt())
action(((value shr 28) and 1u).toInt())
action(((value shr 29) and 1u).toInt())
action(((value shr 30) and 1u).toInt())
action(((value shr 31) and 1u).toInt())
}
inline fun ULong.inlineForEachBit(action: (bit: Int) -> Unit) {
val value = this
action(((value shr 0) and 1u).toInt())
action(((value shr 1) and 1u).toInt())
action(((value shr 2) and 1u).toInt())
action(((value shr 3) and 1u).toInt())
action(((value shr 4) and 1u).toInt())
action(((value shr 5) and 1u).toInt())
action(((value shr 6) and 1u).toInt())
action(((value shr 7) and 1u).toInt())
action(((value shr 8) and 1u).toInt())
action(((value shr 9) and 1u).toInt())
action(((value shr 10) and 1u).toInt())
action(((value shr 11) and 1u).toInt())
action(((value shr 12) and 1u).toInt())
action(((value shr 13) and 1u).toInt())
action(((value shr 14) and 1u).toInt())
action(((value shr 15) and 1u).toInt())
action(((value shr 16) and 1u).toInt())
action(((value shr 17) and 1u).toInt())
action(((value shr 18) and 1u).toInt())
action(((value shr 19) and 1u).toInt())
action(((value shr 20) and 1u).toInt())
action(((value shr 21) and 1u).toInt())
action(((value shr 22) and 1u).toInt())
action(((value shr 23) and 1u).toInt())
action(((value shr 24) and 1u).toInt())
action(((value shr 25) and 1u).toInt())
action(((value shr 26) and 1u).toInt())
action(((value shr 27) and 1u).toInt())
action(((value shr 28) and 1u).toInt())
action(((value shr 29) and 1u).toInt())
action(((value shr 30) and 1u).toInt())
action(((value shr 31) and 1u).toInt())
action(((value shr 32) and 1u).toInt())
action(((value shr 33) and 1u).toInt())
action(((value shr 34) and 1u).toInt())
action(((value shr 35) and 1u).toInt())
action(((value shr 36) and 1u).toInt())
action(((value shr 37) and 1u).toInt())
action(((value shr 38) and 1u).toInt())
action(((value shr 39) and 1u).toInt())
action(((value shr 40) and 1u).toInt())
action(((value shr 41) and 1u).toInt())
action(((value shr 42) and 1u).toInt())
action(((value shr 43) and 1u).toInt())
action(((value shr 44) and 1u).toInt())
action(((value shr 45) and 1u).toInt())
action(((value shr 46) and 1u).toInt())
action(((value shr 47) and 1u).toInt())
action(((value shr 48) and 1u).toInt())
action(((value shr 49) and 1u).toInt())
action(((value shr 50) and 1u).toInt())
action(((value shr 51) and 1u).toInt())
action(((value shr 52) and 1u).toInt())
action(((value shr 53) and 1u).toInt())
action(((value shr 54) and 1u).toInt())
action(((value shr 55) and 1u).toInt())
action(((value shr 56) and 1u).toInt())
action(((value shr 57) and 1u).toInt())
action(((value shr 58) and 1u).toInt())
action(((value shr 59) and 1u).toInt())
action(((value shr 60) and 1u).toInt())
action(((value shr 61) and 1u).toInt())
action(((value shr 62) and 1u).toInt())
action(((value shr 63) and 1u).toInt())
}
fun Byte.getBitAt(position: Int): Int {
require(position in 0 until Byte.SIZE_BITS) { "Position must be between 0 and ${Byte.SIZE_BITS - 1}" }
return (this.toInt() ushr position) and 1
}
fun Byte.setBitAt(position: Int, bit: Int): Byte {
require(position in 0 until Byte.SIZE_BITS) { "Position must be between 0 and ${Byte.SIZE_BITS - 1}" }
require(bit == 0 || bit == 1) { "Value must be 0 or 1" }
return if (bit == 1) {
this or ((1 shl position).toByte())
}
else this and (1 shl position).inv().toByte()
}
fun Short.getBitAt(position: Int): Int {
require(position in 0 until Short.MAX_VALUE) { "Position must be between 0 and {${Short.MAX_VALUE - 1}}" }
return ((this.toInt() ushr position) and 1)
}
fun Short.setBitAt(position: Int, value: Int): Short {
require(position in 0 until Short.SIZE_BITS) { "Position must be between 0 and ${Short.SIZE_BITS - 1}" }
require(value == 0 || value == 1) { "Value must be 0 or 1" }
return if (value == 1) {
this or (1 shl position).toShort()
}
else this and (1 shl position).inv().toShort()
}
fun Int.getBitAt(position: Int): Int {
require(position in 0 until Int.SIZE_BITS) { "Position must be between 0 and ${Int.SIZE_BITS - 1}" }
return (this ushr position) and 1
}
fun Int.setBitAt(position: Int, value: Int): Int {
require(position in 0 until Int.SIZE_BITS) { "Position must be between 0 and ${Int.SIZE_BITS - 1}" }
require(value == 0 || value == 1) { "Value must be 0 or 1" }
return if (value == 1) {
this or (1 shl position)
}
else this and (1 shl position).inv()
}
fun Long.getBitAt(position: Int): Int {
require(position in 0 until Long.SIZE_BITS) { "Position must be between 0 and ${Long.SIZE_BITS - 1}" }
return ((this ushr position) and 1L).toInt()
}
fun Long.setBitAt(position: Int, value: Int): Long {
require(position in 0 until Long.SIZE_BITS) { "Position must be between 0 and ${Long.SIZE_BITS - 1}" }
require(value == 0 || value == 1) { "Value must be 0 or 1" }
return if (value == 1) {
this or (1L shl position)
}
else this and (1L shl position).inv()
}
fun Char.getBitAt(position: Int): Int {
require(position in 0 until Short.SIZE_BITS) { "Position must be between 0 and ${Short.SIZE_BITS - 1}" }
return (code ushr position) and 1
}
fun Char.setBitAt(position: Int, value: Int): Char {
require(position in 0 until Short.SIZE_BITS) { "Position must be between 0 and ${Short.SIZE_BITS - 1}" }
require(value == 0 || value == 1) { "Value must be 0 or 1" }
return if (value == 1) {
(code or (1 shl position)).toChar()
}
else (code and (1 shl position)).inv().toChar()
}
fun UByte.getBitAt(position: Int): Int {
require(position in 0 until UByte.SIZE_BITS) { "Position must be between 0 and ${UByte.SIZE_BITS - 1}" }
return (this.toInt() ushr position) and 1
}
fun UByte.setBitAt(position: Int, value: Int): UByte {
require(position in 0 until UByte.SIZE_BITS) { "Position must be between 0 and ${UByte.SIZE_BITS - 1}" }
require(value == 0 || value == 1) { "Value must be 0 or 1" }
return if (value == 1) {
(this.toInt() or (1 shl position)).toUByte()
}
else (this.toInt() and (1 shl position)).inv().toUByte()
}
fun UShort.getBitAt(position: Int): Int {
require(position in 0 until UShort.SIZE_BITS) { "Position must be between 0 and ${UShort.SIZE_BITS - 1}" }
return (this.toInt() ushr position) and 1
}
fun UShort.setBitAt(position: Int, value: Int): UShort {
require(position in 0 until UShort.SIZE_BITS) { "Position must be between 0 and ${UShort.SIZE_BITS - 1}" }
require(value == 0 || value == 1) { "Value must be 0 or 1" }
return if (value == 1) {
(this.toInt() or (1 shl position)).toUShort()
}
else (this.toInt() and (1 shl position)).inv().toUShort()
}
fun UInt.getBitAt(position: Int): Int {
require(position in 0 until UInt.SIZE_BITS) { "Position must be between 0 and ${UInt.SIZE_BITS - 1}" }
return ((this shr position) and 1u).toInt()
}
fun UInt.setBitAt(position: Int, value: Int): UInt {
require(position in 0 until UInt.SIZE_BITS) { "Position must be between 0 and ${UInt.SIZE_BITS - 1}" }
require(value == 0 || value == 1) { "Value must be 0 or 1" }
return if (value == 1) {
this or (1u shl position)
}
else (this and (1u shl position)).inv()
}
fun ULong.getBitAt(position: Int): Int {
require(position in 0 until ULong.SIZE_BITS) { "Position must be between 0 and ${ULong.SIZE_BITS - 1}" }
return ((this shr position) and 1u).toInt()
}
fun ULong.setBitAt(position: Int, value: Int): ULong {
require(position in 0 until ULong.SIZE_BITS) { "Position must be between 0 and ${ULong.SIZE_BITS - 1}" }
require(value == 0 || value == 1) { "Value must be 0 or 1" }
return if (value == 1) {
(this or (1uL shl position))
}
else (this and (1uL shl position)).inv()
}
fun Byte.flipBitAt(position: Int): Byte {
require(position in 0 until Byte.SIZE_BITS) { "Position must be between 0 and ${Byte.SIZE_BITS - 1}" }
return this xor ((1 shl position).toByte())
}
fun Short.flipBitAt(position: Int): Short {
require(position in 0 until Byte.SIZE_BITS) { "Position must be between 0 and ${Byte.SIZE_BITS - 1}" }
return this xor (1 shl position).toShort()
}
fun Int.flipBitAt(position: Int): Int {
require(position in 0 until Int.SIZE_BITS) { "Position must be between 0 and ${Int.SIZE_BITS - 1}" }
return this xor (1 shl position)
}
fun Long.flipBitAt(position: Int): Long {
require(position in 0 until Long.SIZE_BITS) { "Position must be between 0 and ${Long.SIZE_BITS - 1}" }
return this xor (1L shl position)
}
fun Char.flipBitAt(position: Int): Char {
require(position in 0 until Char.SIZE_BITS) { "Position must be between 0 and ${Char.SIZE_BITS - 1}" }
return (code xor (1 shl position)).toChar()
}
fun UByte.flipBitAt(position: Int): UByte {
require(position in 0 until UByte.SIZE_BITS) { "Position must be between 0 and ${UByte.SIZE_BITS - 1}" }
return (this.toInt() xor (1 shl position)).toUByte()
}
fun UShort.flipBitAt(position: Int): UShort {
require(position in 0 until UShort.SIZE_BITS) { "Position must be between 0 and ${UShort.SIZE_BITS - 1}" }
return (this.toInt() xor (1 shl position)).toUShort()
}
fun UInt.flipBitAt(position: Int): UInt {
require(position in 0 until UInt.SIZE_BITS) { "Position must be between 0 and ${UInt.SIZE_BITS - 1}" }
return (this xor (1u shl position))
}
fun ULong.flipBitAt(position: Int): ULong {
require(position in 0 until ULong.SIZE_BITS) { "Position must be between 0 and ${ULong.SIZE_BITS - 1}" }
return (this xor (1uL shl position))
}
fun Byte.toBitArray(): IntArray {
val array = IntArray(Byte.SIZE_BITS)
for (i in 0 until Byte.SIZE_BITS) {
array[i] = (this.toInt() ushr i) and 1
}
return array
}
fun Short.toBitArray(): IntArray {
val array = IntArray(Short.SIZE_BITS)
for (i in 0 until Short.SIZE_BITS) {
array[i] = (this.toInt() ushr i) and 1
}
return array
}
fun Int.toBitArray(): IntArray {
val array = IntArray(Int.SIZE_BITS)
for (i in 0 until Int.SIZE_BITS) {
array[i] = (this ushr i) and 1
}
return array
}
fun Long.toBitArray(): IntArray {
val array = IntArray(Long.SIZE_BITS)
for (i in 0 until Long.SIZE_BITS) {
array[i] = ((this ushr i) and 1L).toInt()
}
return array
}
fun Char.toBitArray(): IntArray {
val array = IntArray(Char.SIZE_BITS)
val value = code
for (i in 0 until Char.SIZE_BITS) {
array[i] = (value ushr i) and 1
}
return array
}
fun UByte.toBitArray(): IntArray {
val array = IntArray(UByte.SIZE_BITS)
for (i in 0 until UByte.SIZE_BITS) {
array[i] = (this.toInt() ushr i) and 1
}
return array
}
fun UShort.toBitArray(): IntArray {
val array = IntArray(UShort.SIZE_BITS)
for (i in 0 until UShort.SIZE_BITS) {
array[i] = (this.toInt() ushr i) and 1
}
return array
}
fun UInt.toBitArray(): IntArray {
val array = IntArray(UInt.SIZE_BITS)
for (i in 0 until UInt.SIZE_BITS) {
array[i] = ((this shr i) and 1u).toInt()
}
return array
}
fun ULong.toBitArray(): IntArray {
val array = IntArray(ULong.SIZE_BITS)
for (i in 0 until ULong.SIZE_BITS) {
array[i] = ((this shr i) and 1u).toInt()
}
return array
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment