using System;
using System.Numerics;
class Program
{
/// Rounds an unsigned integer to the nearest power of 2.
/// <returns>The nearest power of 2</returns>
static uint RoundToNearestPowerOf2(uint n)
{
if (n == 0) return 0;
uint prevPower = 1u << (31 - BitOperations.LeadingZeroCount(n));
uint nextPower = prevPower << 1;
return (n - prevPower < nextPower - n) ? prevPower : nextPower;
}
static void Main()
{
uint num = 37;
Console.WriteLine($"Nearest power of 2: {RoundToNearestPowerOf2(num)}");
}
}
/*
run:
Nearest power of 2: 32
*/