Contact: aviboots(AT)netvision.net.il
41,163 questions
53,655 answers
573 users
fun roundToPreviousPowerOf2(n: Int): Int { return if (n <= 0) 0 else 1 shl (31 - n.countLeadingZeroBits()) } fun main() { val num = 21 println("Previous power of 2: ${roundToPreviousPowerOf2(num)}") } /* run: Previous power of 2: 16 */