GSF.BitMath.RoundUpToNearestPowerOfTwo C# (CSharp) Method

RoundUpToNearestPowerOfTwo() public static method

Rounds a number up to the nearest power of 2. If the value is a power of two, the same value is returned. If the value is larger than the largest power of 2. It is rounded down.
Method based on a method found at: http://graphics.stanford.edu/~seander/bithacks.htm Subtitle: Round up to the next highest power of 2
public static RoundUpToNearestPowerOfTwo ( uint value ) : uint
value uint
return uint
        public static uint RoundUpToNearestPowerOfTwo(uint value)
        {
            if (value == 0)
                return 1;
            if (value > (1u << 30))
                return 1u << 31;
            value--;
            value |= value >> 1;
            value |= value >> 2;
            value |= value >> 4;
            value |= value >> 8;
            value |= value >> 16;
            value++;
            return value;
        }

Same methods

BitMath::RoundUpToNearestPowerOfTwo ( ulong value ) : ulong