Skip to content

Commit e05933c

Browse files
committed
Update atcoder/internal_bit.hpp to use more efficient bit_ceil
1 parent 864245a commit e05933c

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

atcoder/internal_bit.hpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ using std::bit_ceil;
2121

2222
// @return same with std::bit::bit_ceil
2323
unsigned int bit_ceil(unsigned int n) {
24-
unsigned int x = 1;
25-
while (x < (unsigned int)(n)) x *= 2;
26-
return x;
24+
if (n == 0) return 1;
25+
n--;
26+
n |= n >> 1;
27+
n |= n >> 2;
28+
n |= n >> 4;
29+
n |= n >> 8;
30+
n |= n >> 16;
31+
return n + 1;
2732
}
2833

2934
#endif

0 commit comments

Comments
 (0)