Aseba  1.5.5
power-of-two.h
1 /*
2  Aseba - an event-based framework for distributed robot control
3  Copyright (C) 2007--2016:
4  Stephane Magnenat <stephane at magnenat dot net>
5  (http://stephane.magnenat.net)
6  and other contributors, see authors.txt for details
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation, version 3 of the License.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef __POWER_OF_TWO_H
22 #define __POWER_OF_TWO_H
23 
24 // helper functions for power of two numbers
25 namespace Aseba
26 {
29 
31  template <typename T>
32  bool isPOT(T number)
33  {
34  if (number == 0)
35  return false;
36  while ((number & 1) == 0)
37  number >>= 1;
38  return number == 1;
39  }
40 
42  template <typename T>
43  unsigned shiftFromPOT(T number)
44  {
45  unsigned i = 0;
46  if (number == 0)
47  return 0;
48  while ((number & 1) == 0)
49  {
50  number >>= 1;
51  i++;
52  }
53  return i;
54  }
55 
58 } // namespace Aseba
59 
60 #endif
bool isPOT(T number)
Return true if number is a power of two.
Definition: power-of-two.h:32
unsigned shiftFromPOT(T number)
If number is a power of two, number = (1 << shift) and this function returns shift, otherwise return value is undefined.
Definition: power-of-two.h:43
Definition: analysis.cpp:26