add extra bit helper methods for setting/getting smaller integer-type values in bitfields

This commit is contained in:
Gered 2013-09-08 12:27:19 -04:00
parent 1dfebf08ee
commit d7cfac6632
2 changed files with 130 additions and 0 deletions

View file

@ -255,6 +255,34 @@ namespace Blarg.GameFramework
return (n != 0) && ((n & (n - 1)) == 0);
}
/// <summary>
/// Returns the given integer value raised to the given power.
/// </summary>
/// <returns>The number raised to the given power.</returns>
/// <param name="number">The number to raise to a given power.</param>
/// <param name="power">The power to raise the given number with.</param>
public static long Pow(long number, uint power)
{
long result = 1;
for (var i = 0; i < power; ++i)
result *= number;
return result;
}
/// <summary>
/// Returns the given integer value raised to the given power.
/// </summary>
/// <returns>The number raised to the given power.</returns>
/// <param name="number">The number to raise to a given power.</param>
/// <param name="power">The power to raise the given number with.</param>
public static int Pow(int number, uint power)
{
int result = 1;
for (var i = 0; i < power; ++i)
result *= number;
return result;
}
/// <summary>
/// Linearly interpolates between two values.
/// </summary>

View file

@ -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
}
}