diff options
author | skidd13 <skidd13@openttd.org> | 2007-11-28 21:59:06 +0000 |
---|---|---|
committer | skidd13 <skidd13@openttd.org> | 2007-11-28 21:59:06 +0000 |
commit | 6e511188aef91874ac86281bbc9c25badf9e225c (patch) | |
tree | 7b0c38dfbb177dccff60ecfc27c67e9f9f4a09f1 /src | |
parent | 981865dbccd85e157be745d6e1eb3da535163111 (diff) | |
download | openttd-6e511188aef91874ac86281bbc9c25badf9e225c.tar.xz |
(svn r11538) -Codechange: Rewrite GetNthSetBit in a more uncontroversial way and add its documentation
Diffstat (limited to 'src')
-rw-r--r-- | src/town_gui.cpp | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/src/town_gui.cpp b/src/town_gui.cpp index 3aa49f2d1..d846f4ee9 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -109,15 +109,22 @@ uint GetMaskOfTownActions(int *nump, PlayerID pid, const Town *t) 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; } |