mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-07-20 10:56:19 +01:00
(svn r11538) -Codechange: Rewrite GetNthSetBit in a more uncontroversial way and add its documentation
This commit is contained in:
parent
981865dbcc
commit
6e511188ae
@ -109,15 +109,22 @@ uint GetMaskOfTownActions(int *nump, PlayerID pid, const Town *t)
|
|||||||
return buttons;
|
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)
|
static int GetNthSetBit(uint32 bits, int n)
|
||||||
{
|
{
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
if (n >= 0) {
|
if (n >= 0) {
|
||||||
do {
|
for (uint i = 0; bits != 0; bits >>= 1, i++) {
|
||||||
if (bits & 1 && --n < 0) return i;
|
if (bits & 1) n--;
|
||||||
i++;
|
if (n < 0) return i;
|
||||||
} while (bits >>= 1);
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user