mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-04-12 03:57:32 +00:00
Extension methods for getting upper and lower 4 bits in a byte
This commit is contained in:
parent
b00ee4dccb
commit
6b23238162
2 changed files with 60 additions and 40 deletions
|
|
@ -3,41 +3,53 @@ using NUnit.Framework;
|
|||
|
||||
namespace ICD.Common.Utils.Tests.Extensions
|
||||
{
|
||||
[TestFixture]
|
||||
public sealed class ByteExtensionsTest
|
||||
{
|
||||
[TestCase(false, 0, 0)]
|
||||
[TestCase(true, 1, 0)]
|
||||
[TestCase(false, 247, 3)]
|
||||
[TestCase(true, 255, 3)]
|
||||
public void GetBitTest(bool expected, byte value, int index)
|
||||
{
|
||||
Assert.AreEqual(expected, value.GetBit(index));
|
||||
}
|
||||
[TestFixture]
|
||||
public sealed class ByteExtensionsTest
|
||||
{
|
||||
[TestCase(false, 0, 0)]
|
||||
[TestCase(true, 1, 0)]
|
||||
[TestCase(false, 247, 3)]
|
||||
[TestCase(true, 255, 3)]
|
||||
public void GetBitTest(bool expected, byte value, int index)
|
||||
{
|
||||
Assert.AreEqual(expected, value.GetBit(index));
|
||||
}
|
||||
|
||||
[TestCase(1, 0, 0, true)]
|
||||
[TestCase(0, 1, 0, false)]
|
||||
[TestCase(247, 255, 3, false)]
|
||||
[TestCase(255, 247, 3, true)]
|
||||
public void SetBitTest(byte expected, byte value, int index, bool bitValue)
|
||||
{
|
||||
Assert.AreEqual(expected, value.SetBit(index, bitValue));
|
||||
}
|
||||
[TestCase(1, 0, 0, true)]
|
||||
[TestCase(0, 1, 0, false)]
|
||||
[TestCase(247, 255, 3, false)]
|
||||
[TestCase(255, 247, 3, true)]
|
||||
public void SetBitTest(byte expected, byte value, int index, bool bitValue)
|
||||
{
|
||||
Assert.AreEqual(expected, value.SetBit(index, bitValue));
|
||||
}
|
||||
|
||||
[TestCase(1, 0, 0)]
|
||||
[TestCase(1, 1, 0)]
|
||||
[TestCase(255, 247, 3)]
|
||||
public void SetBitOnTest(byte expected, byte value, int index)
|
||||
{
|
||||
Assert.AreEqual(expected, value.SetBitOn(index));
|
||||
}
|
||||
[TestCase(1, 0, 0)]
|
||||
[TestCase(1, 1, 0)]
|
||||
[TestCase(255, 247, 3)]
|
||||
public void SetBitOnTest(byte expected, byte value, int index)
|
||||
{
|
||||
Assert.AreEqual(expected, value.SetBitOn(index));
|
||||
}
|
||||
|
||||
[TestCase(0, 0, 0)]
|
||||
[TestCase(0, 1, 0)]
|
||||
[TestCase(247, 255, 3)]
|
||||
public void SetBitOffTest(byte expected, byte value, int index)
|
||||
{
|
||||
Assert.AreEqual(expected, value.SetBitOff(index));
|
||||
}
|
||||
}
|
||||
[TestCase(0, 0, 0)]
|
||||
[TestCase(0, 1, 0)]
|
||||
[TestCase(247, 255, 3)]
|
||||
public void SetBitOffTest(byte expected, byte value, int index)
|
||||
{
|
||||
Assert.AreEqual(expected, value.SetBitOff(index));
|
||||
}
|
||||
|
||||
[TestCase(0xFF, 0x0F)]
|
||||
public void GetLower4BitsTest(byte b, byte expected)
|
||||
{
|
||||
Assert.AreEqual(expected, b.GetLower4Bits());
|
||||
}
|
||||
|
||||
[TestCase(0xFF, 0xF0)]
|
||||
public void GetUpper4BitsTest(byte b, byte expected)
|
||||
{
|
||||
Assert.AreEqual(expected, b.GetUpper4Bits());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,22 +7,20 @@ namespace ICD.Common.Utils.Extensions
|
|||
public static bool GetBit(this byte b, int n)
|
||||
{
|
||||
if (n > 7 || n < 0)
|
||||
throw new ArgumentException();
|
||||
throw new ArgumentOutOfRangeException("n");
|
||||
|
||||
return (b & (1 << n)) == (1 << n);
|
||||
}
|
||||
|
||||
public static byte SetBit(this byte b, int n, bool v)
|
||||
{
|
||||
if (v)
|
||||
return SetBitOn(b, n);
|
||||
return SetBitOff(b, n);
|
||||
return v ? SetBitOn(b, n) : SetBitOff(b, n);
|
||||
}
|
||||
|
||||
public static byte SetBitOn(this byte b, int n)
|
||||
{
|
||||
if (n > 7 || n < 0)
|
||||
throw new ArgumentException();
|
||||
throw new ArgumentOutOfRangeException("n");
|
||||
|
||||
return (byte)(b | (1 << n));
|
||||
}
|
||||
|
|
@ -30,9 +28,19 @@ namespace ICD.Common.Utils.Extensions
|
|||
public static byte SetBitOff(this byte b, int n)
|
||||
{
|
||||
if (n > 7 || n < 0)
|
||||
throw new ArgumentException();
|
||||
throw new ArgumentOutOfRangeException("n");
|
||||
|
||||
return (byte)(b & ~(1 << n));
|
||||
}
|
||||
|
||||
public static byte GetLower4Bits(this byte b)
|
||||
{
|
||||
return (byte)(b & 15);
|
||||
}
|
||||
|
||||
public static byte GetUpper4Bits(this byte b)
|
||||
{
|
||||
return (byte)(b >> 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue