GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
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 
15 long G_math_max_pow2(const long n)
16 {
17  long p2, n1;
18 
19  n1 = n >> 1;
20  p2 = 1;
21  while (n1 > 0) {
22  n1 >>= 1;
23  p2 <<= 1;
24  }
25  if (p2 < n)
26  p2 <<= 1;
27 
28  return (p2);
29 }
30 
31 /*!
32  * \fn long G_math_min_pow2 (const long n)
33  *
34  * \brief Finds largest power of 2 <= <b>n</b>
35  *
36  * Finds largest power of 2 <= <b>n</b>.
37  *
38  * \param[in] n
39  * \return long
40  */
41 
42 long G_math_min_pow2(const long n)
43 {
44  long p2, n1;
45 
46  n1 = n >> 1;
47  p2 = 1;
48  while (n1 > 0) {
49  n1 >>= 1;
50  p2 <<= 1;
51  }
52 
53  return (p2);
54 }
long G_math_max_pow2(const long n)
Finds least power of 2 >= n
Definition: max_pow2.c:15
long G_math_min_pow2(const long n)
Finds largest power of 2 <= n
Definition: max_pow2.c:42