brucemax
brucemax

Reputation: 962

Kotlin: How to get the value of a bit at a certain position from a Byte?

I found solution for java:

public byte getBit(byte value, int position) {
   return (value >> position) & 1;
}

But how it is in Kotlin?

Upvotes: 8

Views: 3855

Answers (1)

CryptoFool
CryptoFool

Reputation: 23129

The Kotlin equivalent is:

fun getBit(value: Int, position: Int): Int {
    return (value shr position) and 1;
}

Upvotes: 16

Related Questions