(svn r11538) -Codechange: Rewrite GetNthSetBit in a more uncontroversial way and add its documentation
authorskidd13
Wed, 28 Nov 2007 21:59:06 +0000
changeset 8478 11bf86f9d6b7
parent 8477 b7428cf1d567
child 8479 a1ab2afafb84
(svn r11538) -Codechange: Rewrite GetNthSetBit in a more uncontroversial way and add its documentation
src/town_gui.cpp
--- a/src/town_gui.cpp	Wed Nov 28 15:42:52 2007 +0000
+++ b/src/town_gui.cpp	Wed Nov 28 21:59:06 2007 +0000
@@ -109,15 +109,22 @@
 	return buttons;
 }
 
+/**
+ * Get the position of the Nth set bit.
+ *
+ * If there is no Nth bit set return -1
+ *
+ * @param bits The value to search in
+ * @param n The Nth set bit from which we want to know the position
+ * @return The position of the Nth set bit
+ */
 static int GetNthSetBit(uint32 bits, int n)
 {
-	int i = 0;
-
 	if (n >= 0) {
-		do {
-			if (bits & 1 && --n < 0) return i;
-			i++;
-		} while (bits >>= 1);
+		for (uint i = 0; bits != 0; bits >>= 1, i++) {
+			if (bits & 1) n--;
+			if (n < 0) return i;
+		}
 	}
 	return -1;
 }