package main
import (
"fmt"
"math/bits"
)
// roundToNearestPowerOf2 returns the power of 2 closest to n.
func roundToNearestPowerOf2(n uint32) uint32 {
if n == 0 {
return 0
}
// Compute the previous power of 2 using leading zero count
prev := uint32(1) << (31 - bits.LeadingZeros32(n))
next := prev << 1
if n-prev < next-n {
return prev
}
return next
}
func main() {
num := uint32(37)
fmt.Printf("Nearest power of 2: %d\n", roundToNearestPowerOf2(num))
}
/*
run:
Nearest power of 2: 32
*/