GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
max_pow2.c
Go to the documentation of this file.
1 #include <grass/gis.h>
2 #include <grass/gmath.h>
3 
4 /*!
5  * \fn long G_math_max_pow2 (const long n)
6  *
7  * \brief Finds least power of 2 >= <b>n</b>
8  *
9  * Finds least power of 2 >= <b>n</b>.
10  *
11  * \param[in] n
12  * \return long
13  */
14 long G_math_max_pow2(const long n)
15 {
16  long p2, n1;
17 
18  n1 = n >> 1;
19  p2 = 1;
20  while (n1 > 0) {
21  n1 >>= 1;
22  p2 <<= 1;
23  }
24  if (p2 < n)
25  p2 <<= 1;
26 
27  return (p2);
28 }
29 
30 /*!
31  * \fn long G_math_min_pow2 (const long n)
32  *
33  * \brief Finds largest power of 2 <= <b>n</b>
34  *
35  * Finds largest power of 2 <= <b>n</b>.
36  *
37  * \param[in] n
38  * \return long
39  */
40 long G_math_min_pow2(const long n)
41 {
42  long p2, n1;
43 
44  n1 = n >> 1;
45  p2 = 1;
46  while (n1 > 0) {
47  n1 >>= 1;
48  p2 <<= 1;
49  }
50 
51  return (p2);
52 }
long G_math_max_pow2(const long n)
Finds least power of 2 >= n
Definition: max_pow2.c:14
long G_math_min_pow2(const long n)
Finds largest power of 2 <= n
Definition: max_pow2.c:40