summaryrefslogtreecommitdiff
path: root/src/misc.cpp
diff options
context:
space:
mode:
authormaedhros <maedhros@openttd.org>2007-06-12 22:13:49 +0000
committermaedhros <maedhros@openttd.org>2007-06-12 22:13:49 +0000
commitf8ae742da455067385b1255d9b605087b2141295 (patch)
treef4b3d4a7be99176b7ebd3e1980b37402b5128f4c /src/misc.cpp
parent0e53fd6d328a128799034e155a92cf44fddc1cd0 (diff)
downloadopenttd-f8ae742da455067385b1255d9b605087b2141295.tar.xz
(svn r10122) -Codechange: Add a CountBitsSet function and use it to replace some less efficient loops.
Diffstat (limited to 'src/misc.cpp')
-rw-r--r--src/misc.cpp15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/misc.cpp b/src/misc.cpp
index 2dd2750d0..d143980c4 100644
--- a/src/misc.cpp
+++ b/src/misc.cpp
@@ -270,6 +270,21 @@ int FindFirstBit(uint32 value)
return i;
}
+int CountBitsSet(uint32 value)
+{
+ int num;
+
+ /* This loop is only called once for every bit set by clearing the lowest
+ * bit in each loop. The number of bits is therefore equal to the number of
+ * times the loop was called. It was found at the following website:
+ * http://graphics.stanford.edu/~seander/bithacks.html */
+
+ for (num = 0; value != 0; num++) {
+ value &= value - 1;
+ }
+
+ return num;
+}
static void Save_NAME()
{