power-of-two.h
Go to the documentation of this file.
00001 /*
00002         Aseba - an event-based framework for distributed robot control
00003         Copyright (C) 2007--2012:
00004                 Stephane Magnenat <stephane at magnenat dot net>
00005                 (http://stephane.magnenat.net)
00006                 and other contributors, see authors.txt for details
00007         
00008         This program is free software: you can redistribute it and/or modify
00009         it under the terms of the GNU Lesser General Public License as published
00010         by the Free Software Foundation, version 3 of the License.
00011         
00012         This program is distributed in the hope that it will be useful,
00013         but WITHOUT ANY WARRANTY; without even the implied warranty of
00014         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015         GNU Lesser General Public License for more details.
00016         
00017         You should have received a copy of the GNU Lesser General Public License
00018         along with this program. If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 #ifndef __POWER_OF_TWO_H
00022 #define __POWER_OF_TWO_H
00023 
00024 // helper functions for power of two numbers
00025 namespace Aseba
00026 {
00029         
00031         template <typename T>
00032         bool isPOT(T number)
00033         {
00034                 if (number == 0)
00035                         return true;
00036                 while ((number & 1) == 0)
00037                         number >>= 1;
00038                 return number == 1;
00039         }
00040         
00042         template <typename T>
00043         unsigned shiftFromPOT(T number)
00044         {
00045                 unsigned i = 0;
00046                 if (number == 0)
00047                         return 0;
00048                 while ((number & 1) == 0)
00049                 {
00050                         number >>= 1;
00051                         i++;
00052                 }
00053                 return i;
00054         }
00055 
00058 }; // Aseba
00059 
00060 #endif


aseba
Author(s): Stéphane Magnenat
autogenerated on Sun Oct 5 2014 23:46:39