From 1414e34291b45a652f1af1d9c06579cd9556ad5d Mon Sep 17 00:00:00 2001 From: Michael Lidgren Date: Wed, 25 Nov 2015 00:15:39 +0100 Subject: [PATCH] Added missing NetUtility.BitsToHoldUInt64 --- Lidgren.Network/NetUtility.cs | 12 ++++++++++++ UnitTests/MiscTests.cs | 3 +++ 2 files changed, 15 insertions(+) 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"); } } }