How to toggle a bit at specific position in Kotlin

1 Answer

0 votes
fun getBits(n: Int): String {
    return n.toString(2)
}

fun main() {
    var n = 365
    val pos = 2

    println(getBits(n))

    n = n xor (1 shl pos)

    println(getBits(n))
}


/*
run:
     
101101101
101101001
      
*/

 



answered Apr 3 by avibootz
...