All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.dev.datlag.sekret.common.ExtendInt.kt Maven / Gradle / Ivy

The newest version!
package dev.datlag.sekret.common

/**
 * Returns the value obtained by rotating the two's complement binary representation of the specified [Int] value
 * right by the specified number of bits.
 * (Bits shifted out of the right hand, or low-order, side reenter on the left, or high-order.)
 */
internal fun Int.rotateRight(distance: Int): Int {
    return this.ushr(distance) or (this shl -distance)
}

/**
 * Converts an [Int] to an array of [Byte] using the big-endian conversion.
 * (The [Int] will be converted into 4 bytes)
 */
internal fun Int.toBytes(): Array {
    val result = ByteArray(4)
    result[0] = (this shr 24).toByte()
    result[1] = (this shr 16).toByte()
    result[2] = (this shr 8).toByte()
    result[3] = this.toByte()
    return result.toTypedArray()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy