public void BitCountAllBitsSetShouldReturnBitWidth() { Assert.AreEqual(8, BitMath.BitCount((byte)0xff), "byte"); Assert.AreEqual(8, BitMath.BitCount((sbyte)-1), "sbyte"); Assert.AreEqual(16, BitMath.BitCount((ushort)0xffff), "ushort"); Assert.AreEqual(16, BitMath.BitCount((short)-1), "short"); Assert.AreEqual(32, BitMath.BitCount((uint)0xffffffff), "uint"); Assert.AreEqual(32, BitMath.BitCount((int)-1), "int"); Assert.AreEqual(64, BitMath.BitCount((ulong)0xffffffffffffffff), "ulong"); Assert.AreEqual(64, BitMath.BitCount((long)-1), "long"); Assert.AreEqual(128, BitMath.BitCount(new UInt128(0xffffffffffffffff, 0xffffffffffffffff)), "UInt128"); }
public void BitCountMsbSetShouldReturn1() { Assert.AreEqual(1, BitMath.BitCount((byte)0x80), "byte"); Assert.AreEqual(1, BitMath.BitCount(sbyte.MinValue), "sbyte"); Assert.AreEqual(1, BitMath.BitCount((ushort)0x8000), "ushort"); Assert.AreEqual(1, BitMath.BitCount(short.MinValue), "short"); Assert.AreEqual(1, BitMath.BitCount((uint)0x80000000), "uint"); Assert.AreEqual(1, BitMath.BitCount(int.MinValue), "int"); Assert.AreEqual(1, BitMath.BitCount((ulong)0x8000000000000000), "ulong"); Assert.AreEqual(1, BitMath.BitCount(long.MinValue), "long"); Assert.AreEqual(1, BitMath.BitCount(new UInt128(0x8000000000000000, 0)), "UInt128"); }
public void BitCount1ShouldReturn1() { Assert.AreEqual(1, BitMath.BitCount((byte)1), "byte"); Assert.AreEqual(1, BitMath.BitCount((sbyte)1), "sbyte"); Assert.AreEqual(1, BitMath.BitCount((ushort)1), "ushort"); Assert.AreEqual(1, BitMath.BitCount((short)1), "short"); Assert.AreEqual(1, BitMath.BitCount((uint)1), "uint"); Assert.AreEqual(1, BitMath.BitCount((int)1), "int"); Assert.AreEqual(1, BitMath.BitCount((ulong)1), "ulong"); Assert.AreEqual(1, BitMath.BitCount((long)1), "long"); Assert.AreEqual(1, BitMath.BitCount((UInt128)1), "UInt128"); }
public void BitCountByteShouldMatchWider() { byte v = byte.MinValue; while (true) { Assert.AreEqual(BitMath.BitCount(v), BitMath.BitCount(unchecked ((sbyte)v)), "sbyte"); Assert.AreEqual(BitMath.BitCount(v), BitMath.BitCount((ushort)v), "ushort"); Assert.AreEqual(BitMath.BitCount(v), BitMath.BitCount((short)v), "short"); Assert.AreEqual(BitMath.BitCount(v), BitMath.BitCount((uint)v), "uint"); Assert.AreEqual(BitMath.BitCount(v), BitMath.BitCount((int)v), "int"); Assert.AreEqual(BitMath.BitCount(v), BitMath.BitCount((ulong)v), "ulong"); Assert.AreEqual(BitMath.BitCount(v), BitMath.BitCount((long)v), "long"); Assert.AreEqual(BitMath.BitCount(v), BitMath.BitCount((UInt128)v), "UInt128"); if (v == byte.MaxValue) { break; } v++; } }