public void UInt64_ShouldReturnExpectedResult(ulong input, bool expected) { // Act bool actual = BinaryMath.IsPowerOfTwoOrZero(input); // Assert Assert.Equal(expected, actual); }
/// <summary> /// Calculates the padding needed to get the specified offset to a multiple of the specified boundary. /// </summary> /// <param name="offset">The offset.</param> /// <param name="boundary">The boundary.</param> /// <returns>The number of padding bytes to add.</returns> public static long GetPadding(long offset, int boundary) { #region Contract if (boundary <= 0) { throw new ArgumentOutOfRangeException(nameof(boundary)); } #endregion if (BinaryMath.IsPowerOfTwoOrZero(boundary)) { return((long)BinaryMath.GetPadding(unchecked ((ulong)offset), boundary)); } else { return(IntegerMath.Modulo(boundary - IntegerMath.Modulo(offset, boundary), boundary)); } }
/// <summary> /// Calculates the padding needed to get the specified offset to a multiple of the specified boundary. /// </summary> /// <param name="offset">The offset.</param> /// <param name="boundary">The boundary.</param> /// <returns>The number of padding bytes to add.</returns> public static ulong GetPadding(ulong offset, int boundary) { #region Contract if (boundary <= 0) { throw new ArgumentOutOfRangeException(nameof(boundary)); } #endregion ulong b = unchecked ((ulong)boundary); if (BinaryMath.IsPowerOfTwoOrZero(boundary)) { return((~offset + 1) & (b - 1)); } else { return((b - (offset % b)) % b); } }
/// <summary> /// Determines whether a value is a positive power of two. /// </summary> /// <param name="value">The value to test.</param> /// <returns><see langword="true"/> when <paramref name="value"/> is a positive power of two; /// otherwise, <see langword="false"/>.</returns> public static bool IsPowerOfTwo(long value) { return(value > 0 && BinaryMath.IsPowerOfTwoOrZero(unchecked ((ulong)value))); }
/// <summary> /// Determines whether a value is a positive power of two, or zero. /// </summary> /// <param name="value">The value to test.</param> /// <returns><see langword="true"/> when <paramref name="value"/> is a positive power of two, /// or zero; otherwise, <see langword="false"/>.</returns> public static bool IsPowerOfTwoOrZero(int value) { return(BinaryMath.IsPowerOfTwoOrZero((long)value)); }
/// <summary> /// Determines whether a value is a power of two. /// </summary> /// <param name="value">The value to test.</param> /// <returns><see langword="true"/> when <paramref name="value"/> is a power of two; /// otherwise, <see langword="false"/>.</returns> public static bool IsPowerOfTwo(ulong value) { return(value != 0 && BinaryMath.IsPowerOfTwoOrZero(value)); }