summary refs log tree commit diff
path: root/crypto/src/util/Integers.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/util/Integers.cs')
-rw-r--r--crypto/src/util/Integers.cs21
1 files changed, 21 insertions, 0 deletions
diff --git a/crypto/src/util/Integers.cs b/crypto/src/util/Integers.cs
index eff7056b6..87ba9cbb0 100644
--- a/crypto/src/util/Integers.cs
+++ b/crypto/src/util/Integers.cs
@@ -79,6 +79,27 @@ namespace Org.BouncyCastle.Utilities
 #endif
         }
 
+        public static int PopCount(int i)
+        {
+            return PopCount((uint)i);
+        }
+
+        [CLSCompliant(false)]
+        public static int PopCount(uint u)
+        {
+#if NETCOREAPP3_0_OR_GREATER
+            return BitOperations.PopCount(u);
+#else
+            u -= (u >> 1) & 0x55555555;
+            u = (u & 0x33333333) + ((u >> 2) & 0x33333333);
+            u = (u + (u >> 4)) & 0x0f0f0f0f;
+            u += (u >> 8);
+            u += (u >> 16);
+            u &= 0x3f;
+            return (int)u;
+#endif
+        }
+
         public static int Reverse(int i)
         {
             return (int)Reverse((uint)i);