MyGUI  3.2.1
MyGUI_Bitwise.h
Go to the documentation of this file.
00001 /*
00002  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
00003  * Distributed under the MIT License
00004  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
00005  */
00006 
00007 #ifndef __MYGUI_BITWISE_H__
00008 #define __MYGUI_BITWISE_H__
00009 
00010 #include "MyGUI_Prerequest.h"
00011 
00012 namespace MyGUI
00013 {
00014 
00015     class Bitwise
00016     {
00017     public:
00020         template<typename Type>
00021         static MYGUI_FORCEINLINE Type firstPO2From(Type _value)
00022         {
00023             --_value;
00024             _value |= _value >> 16;
00025             _value |= _value >> 8;
00026             _value |= _value >> 4;
00027             _value |= _value >> 2;
00028             _value |= _value >> 1;
00029             ++_value;
00030             return _value;
00031         }
00032 
00034         template<typename Type>
00035         static MYGUI_FORCEINLINE bool isPO2(Type _value)
00036         {
00037             return (_value & (_value - 1)) == 0;
00038         }
00039 
00043         template<typename Type>
00044         static MYGUI_FORCEINLINE size_t getBitShift(Type _mask)
00045         {
00046             if (_mask == 0)
00047                 return 0;
00048 
00049             size_t result = 0;
00050             while ((_mask & 1) == 0)
00051             {
00052                 ++result;
00053                 _mask >>= 1;
00054             }
00055             return result;
00056         }
00057     };
00058 
00059 } // namespace MyGUI
00060 
00061 #endif // __MYGUI_BITWISE_H__