import Foundation
/// Rounds an integer down to the previous power of 2.
/// - Returns: The previous power of 2 less than or equal to `n`.
func roundToPreviousPowerOf2(_ n: Int) -> Int {
guard n > 0 else { return 0 }
return 1 << (Int.bitWidth - n.leadingZeroBitCount - 1)
}
let num = 21
print("Previous power of 2: \(roundToPreviousPowerOf2(num))")
/*
run:
Previous power of 2: 16
*/