diff --git a/Blarg.GameFramework/Math/MathHelpers.cs b/Blarg.GameFramework/Math/MathHelpers.cs index e40f4e8..7dcf55f 100644 --- a/Blarg.GameFramework/Math/MathHelpers.cs +++ b/Blarg.GameFramework/Math/MathHelpers.cs @@ -255,6 +255,34 @@ namespace Blarg.GameFramework return (n != 0) && ((n & (n - 1)) == 0); } + /// + /// Returns the given integer value raised to the given power. + /// + /// The number raised to the given power. + /// The number to raise to a given power. + /// The power to raise the given number with. + public static long Pow(long number, uint power) + { + long result = 1; + for (var i = 0; i < power; ++i) + result *= number; + return result; + } + + /// + /// Returns the given integer value raised to the given power. + /// + /// The number raised to the given power. + /// The number to raise to a given power. + /// The power to raise the given number with. + public static int Pow(int number, uint power) + { + int result = 1; + for (var i = 0; i < power; ++i) + result *= number; + return result; + } + /// /// Linearly interpolates between two values. /// diff --git a/Blarg.GameFramework/Support/BitExtensions.cs b/Blarg.GameFramework/Support/BitExtensions.cs index 73d20bf..a3100db 100644 --- a/Blarg.GameFramework/Support/BitExtensions.cs +++ b/Blarg.GameFramework/Support/BitExtensions.cs @@ -9,6 +9,12 @@ namespace Blarg.GameFramework.Support public static class BitExtensions { + public static int GetBitmaskFor(uint startBit, uint numBits) + { + int temp = MathHelpers.Pow(2, startBit); + return (temp * (MathHelpers.Pow(2, numBits))) - temp; + } + #region Is Set public static bool IsBitSet(this long bitfield, long bit) @@ -185,6 +191,102 @@ namespace Blarg.GameFramework.Support } #endregion + + #region Bit values + + #region Extraction + + public static long ExtractValue(this long bitfield, long mask, int shift) + { + return (bitfield & mask) >> shift; + } + + public static ulong ExtractValue(this ulong bitfield, ulong mask, int shift) + { + return (bitfield & mask) >> shift; + } + + public static int ExtractValue(this int bitfield, int mask, int shift) + { + return (bitfield & mask) >> shift; + } + + public static uint ExtractValue(this uint bitfield, uint mask, int shift) + { + return (bitfield & mask) >> shift; + } + + public static short ExtractValue(this short bitfield, short mask, int shift) + { + return (short)(((int)bitfield & mask) >> shift); + } + + public static ushort ExtractValue(this ushort bitfield, ushort mask, int shift) + { + return (ushort)(((int)bitfield & mask) >> shift); + } + + public static byte ExtractValue(this byte bitfield, byte mask, int shift) + { + return (byte)(((int)bitfield & mask) >> shift); + } + + #endregion + + #region Setting + + public static long SetValue(this long bitfield, long value, long bitmask, int shift, uint valueMaxBitLength) + { + long maxValue = MathHelpers.Pow(2, valueMaxBitLength) - 1; + long actualValue = (value > maxValue ? maxValue : value) << shift; + return (bitfield | actualValue); + } + + public static ulong SetValue(this ulong bitfield, ulong value, ulong bitmask, int shift, uint valueMaxBitLength) + { + ulong maxValue = (ulong)MathHelpers.Pow((long)2, valueMaxBitLength) - 1; + ulong actualValue = (value > maxValue ? maxValue : value) << shift; + return (bitfield | actualValue); + } + + public static int SetValue(this int bitfield, int value, int bitmask, int shift, uint valueMaxBitLength) + { + int maxValue = MathHelpers.Pow(2, valueMaxBitLength) - 1; + int actualValue = (value > maxValue ? maxValue : value) << shift; + return (bitfield | actualValue); + } + + public static uint SetValue(this uint bitfield, uint value, uint bitmask, int shift, uint valueMaxBitLength) + { + uint maxValue = (uint)MathHelpers.Pow(2, valueMaxBitLength) - 1; + uint actualValue = (value > maxValue ? maxValue : value) << shift; + return (bitfield | actualValue); + } + + public static short SetValue(this short bitfield, short value, short bitmask, int shift, uint valueMaxBitLength) + { + short maxValue = (short)(MathHelpers.Pow(2, valueMaxBitLength) - 1); + int actualValue = (value > maxValue ? maxValue : value) << shift; + return (short)((int)bitfield | actualValue); + } + + public static ushort SetValue(this ushort bitfield, ushort value, ushort bitmask, int shift, uint valueMaxBitLength) + { + ushort maxValue = (ushort)(MathHelpers.Pow(2, valueMaxBitLength) - 1); + int actualValue = (value > maxValue ? maxValue : value) << shift; + return (ushort)((int)bitfield | actualValue); + } + + public static byte SetValue(this byte bitfield, byte value, byte bitmask, int shift, uint valueMaxBitLength) + { + byte maxValue = (byte)(MathHelpers.Pow(2, valueMaxBitLength) - 1); + int actualValue = (value > maxValue ? maxValue : value) << shift; + return (byte)((int)bitfield | actualValue); + } + + #endregion + + #endregion } }