Mercurial > hg > openttd
changeset 7982:a3042fc837e1 draft
(svn r11538) -Codechange: Rewrite GetNthSetBit in a more uncontroversial way and add its documentation
author | skidd13 <skidd13@openttd.org> |
---|---|
date | Wed, 28 Nov 2007 21:59:06 +0000 |
parents | 71f89e3a19f6 |
children | 0d1077a6c3ea |
files | src/town_gui.cpp |
diffstat | 1 files changed, 13 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -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; }