diff --git a/Lidgren.Network/NetUtility.cs b/Lidgren.Network/NetUtility.cs
index 2d58a4b..20ac1e6 100644
--- a/Lidgren.Network/NetUtility.cs
+++ b/Lidgren.Network/NetUtility.cs
@@ -282,6 +282,18 @@ namespace Lidgren.Network
return bits;
}
+ ///
+ /// Returns how many bits are necessary to hold a certain number
+ ///
+ [CLSCompliant(false)]
+ public static int BitsToHoldUInt64(ulong value)
+ {
+ int bits = 1;
+ while ((value >>= 1) != 0)
+ bits++;
+ return bits;
+ }
+
///
/// Returns how many bytes are required to hold a certain number of bits
///
diff --git a/UnitTests/MiscTests.cs b/UnitTests/MiscTests.cs
index cfdd6f2..8334628 100644
--- a/UnitTests/MiscTests.cs
+++ b/UnitTests/MiscTests.cs
@@ -21,6 +21,9 @@ namespace UnitTests
Console.WriteLine("Misc tests OK");
Console.WriteLine("Hex test: " + NetUtility.ToHexString(new byte[]{0xDE,0xAD,0xBE,0xEF}));
+
+ if (NetUtility.BitsToHoldUInt64((ulong)UInt32.MaxValue + 1ul) != 33)
+ throw new NetException("BitsToHoldUInt64 failed");
}
}
}