Ejemplo n.º 1
0
		/// <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;
		}