本文整理汇总了C#中System.UInt32.GetHighestSetBitIndex方法的典型用法代码示例。如果您正苦于以下问题:C# UInt32.GetHighestSetBitIndex方法的具体用法?C# UInt32.GetHighestSetBitIndex怎么用?C# UInt32.GetHighestSetBitIndex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.UInt32
的用法示例。
在下文中一共展示了UInt32.GetHighestSetBitIndex方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Align
/// <summary>
/// Aligns <paramref name="value" /> to the <paramref name="powerOfTwoBase" />.
/// </summary>
/// <param name="value">The unsigned integer value.</param>
/// <param name="powerOfTwoBase">The power of two align base.</param>
/// <returns>Aligned value.</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="powerOfTwoBase" /> is not power of two.</exception>
public static UInt32 Align(this UInt32 value, UInt32 powerOfTwoBase)
{
if (!powerOfTwoBase.IsPowerOfTwo())
{
throw new ArgumentException("Is not power of two", nameof(powerOfTwoBase));
}
if (value == 0)
{
return powerOfTwoBase;
}
// get power
var reminder = value & (powerOfTwoBase - 1);
if (reminder == 0)
{
return value;
}
var power = powerOfTwoBase.GetHighestSetBitIndex();
var multiplier = value >> power;
return (multiplier + 1) << power;
}