00001
00007 #ifndef BITWIZARD_H_
00008 #define BITWIZARD_H_
00009
00011 namespace BitMinion
00012 {
00014 enum BitIndexEnumType
00015 {
00016 BIT_ZERO_INDEX = 0,
00017 BIT_ONE_INDEX = 1,
00018 BIT_TWO_INDEX = 2,
00019 BIT_THREE_INDEX = 3,
00020 BIT_FOUR_INDEX = 4,
00021 BIT_FIVE_INDEX = 5,
00022 BIT_SIX_INDEX = 6,
00023 BIT_SEVEN_INDEX = 7,
00024 BIT_EIGHT_INDEX = 8,
00025 BIT_NINE_INDEX = 9,
00026 BIT_TEN_INDEX = 10,
00027 BIT_ELEVEN_INDEX = 11,
00028 BIT_TWELVE_INDEX = 12,
00029 BIT_THIRTEEN_INDEX = 13,
00030 BIT_FOURTEEN_INDEX = 14,
00031 BIT_FIFTEEN_INDEX = 15,
00032 BIT_SIXTEEN_INDEX = 16,
00033 BIT_SEVENTEEN_INDEX = 17,
00034 BIT_EIGHTEEN_INDEX = 18,
00035 BIT_NINETEEN_INDEX = 19,
00036 BIT_TWENTY_INDEX = 20,
00037 BIT_TWENTYONE_INDEX = 21,
00038 BIT_TWENTYTWO_INDEX = 22,
00039 BIT_TWENTYTHREE_INDEX = 23,
00040 BIT_TWENTYFOUR_INDEX = 24,
00041 BIT_TWENTYFIVE_INDEX = 25,
00042 BIT_TWENTYSIX_INDEX = 26,
00043 BIT_TWENTYSEVEN_INDEX = 27,
00044 BIT_TWENTYEIGHT_INDEX = 28,
00045 BIT_TWENTYNINE_INDEX = 29,
00046 BIT_THIRTY_INDEX = 30,
00047 BIT_THIRTYONE_INDEX = 31,
00048 };
00049
00051 enum BitMaskEnumType
00052 {
00053 BIT_ZERO_MASK = (1 << BIT_ZERO_INDEX),
00054 BIT_ONE_MASK = (1 << BIT_ONE_INDEX),
00055 BIT_TWO_MASK = (1 << BIT_TWO_INDEX),
00056 BIT_THREE_MASK = (1 << BIT_THREE_INDEX),
00057 BIT_FOUR_MASK = (1 << BIT_FOUR_INDEX),
00058 BIT_FIVE_MASK = (1 << BIT_FIVE_INDEX),
00059 BIT_SIX_MASK = (1 << BIT_SIX_INDEX),
00060 BIT_SEVEN_MASK = (1 << BIT_SEVEN_INDEX),
00061 BIT_EIGHT_MASK = (1 << BIT_EIGHT_INDEX),
00062 BIT_NINE_MASK = (1 << BIT_NINE_INDEX),
00063 BIT_TEN_MASK = (1 << BIT_TEN_INDEX),
00064 BIT_ELEVEN_MASK = (1 << BIT_ELEVEN_INDEX),
00065 BIT_TWELVE_MASK = (1 << BIT_TWELVE_INDEX),
00066 BIT_THIRTEEN_MASK = (1 << BIT_THIRTEEN_INDEX),
00067 BIT_FOURTEEN_MASK = (1 << BIT_FOURTEEN_INDEX),
00068 BIT_FIFTEEN_MASK = (1 << BIT_FIFTEEN_INDEX),
00069 BIT_SIXTEEN_MASK = (1 << BIT_SIXTEEN_INDEX),
00070 BIT_SEVENTEEN_MASK = (1 << BIT_SEVENTEEN_INDEX),
00071 BIT_EIGHTEEN_MASK = (1 << BIT_EIGHTEEN_INDEX),
00072 BIT_NINETEEN_MASK = (1 << BIT_NINETEEN_INDEX),
00073 BIT_TWENTY_MASK = (1 << BIT_TWENTY_INDEX),
00074 BIT_TWENTYONE_MASK = (1 << BIT_TWENTYONE_INDEX),
00075 BIT_TWENTYTWO_MASK = (1 << BIT_TWENTYTWO_INDEX),
00076 BIT_TWENTYTHREE_MASK = (1 << BIT_TWENTYTHREE_INDEX),
00077 BIT_TWENTYFOUR_MASK = (1 << BIT_TWENTYFOUR_INDEX),
00078 BIT_TWENTYFIVE_MASK = (1 << BIT_TWENTYFIVE_INDEX),
00079 BIT_TWENTYSIX_MASK = (1 << BIT_TWENTYSIX_INDEX),
00080 BIT_TWENTYSEVEN_MASK = (1 << BIT_TWENTYSEVEN_INDEX),
00081 BIT_TWENTYEIGHT_MASK = (1 << BIT_TWENTYEIGHT_INDEX),
00082 BIT_TWENTYNINE_MASK = (1 << BIT_TWENTYNINE_INDEX),
00083 BIT_THIRTY_MASK = (1 << BIT_THIRTY_INDEX),
00084 BIT_THIRTYONE_MASK = (1 << BIT_THIRTYONE_INDEX),
00085 };
00086
00088 enum BitCountMaskEnumType
00089 {
00090 ONE_BIT_MASK = 0x00000001,
00091 TWO_BIT_MASK = 0x00000003,
00092 THREE_BIT_MASK = 0x00000007,
00093 FOUR_BIT_MASK = 0x0000000F,
00094 FIVE_BIT_MASK = 0x0000001F,
00095 SIX_BIT_MASK = 0x0000003F,
00096 SEVEN_BIT_MASK = 0x0000007F,
00097 EIGHT_BIT_MASK = 0x000000FF,
00098 NINE_BIT_MASK = 0x000001FF,
00099 TEN_BIT_MASK = 0x000003FF,
00100 ELEVEN_BIT_MASK = 0x000007FF,
00101 TWELVE_BIT_MASK = 0x00000FFF,
00102 THIRTEEN_BIT_MASK = 0x00001FFF,
00103 FOURTEEN_BIT_MASK = 0x00003FFF,
00104 FIFTEEN_BIT_MASK = 0x00007FFF,
00105 SIXTEEN_BIT_MASK = 0x0000FFFF,
00106 SEVENTEEN_BIT_MASK = 0x0001FFFF,
00107 EIGHTEEN_BIT_MASK = 0x0003FFFF,
00108 NINETEEN_BIT_MASK = 0x0007FFFF,
00109 TWENTY_BIT_MASK = 0x000FFFFF,
00110 TWENTYONE_BIT_MASK = 0x001FFFFF,
00111 TWENTYTWO_BIT_MASK = 0x003FFFFF,
00112 TWENTYTHREE_BIT_MASK = 0x007FFFFF,
00113 TWENTYFOUR_BIT_MASK = 0x00FFFFFF,
00114 TWENTYFIVE_BIT_MASK = 0x01FFFFFF,
00115 TWENTYSIX_BIT_MASK = 0x03FFFFFF,
00116 TWENTYSEVEN_BIT_MASK = 0x07FFFFFF,
00117 TWENTYEIGHT_BIT_MASK = 0x0FFFFFFF,
00118 TWENTYNINE_BIT_MASK = 0x1FFFFFFF,
00119 THIRTY_BIT_MASK = 0x3FFFFFFF,
00120 THIRTYONE_BIT_MASK = 0x7FFFFFFF,
00121 THIRTYTWO_BIT_MASK = 0xFFFFFFFF,
00122 };
00123 };
00124
00134 template <typename T>
00135 class BitWizard
00136 {
00137 public:
00139 BitWizard();
00141 virtual ~BitWizard();
00142
00149 bool getBit(const BitMinion::BitIndexEnumType& bitIndex, const T& registerValue) const;
00150
00158 bool testBit(const bool& bitValue, const BitMinion::BitIndexEnumType& bitIndex, const T& registerValue) const;
00159
00167 void setBit(const bool& bitValue, const BitMinion::BitIndexEnumType& bitIndex, T& registerValue);
00168
00176 void toggleBit(const BitMinion::BitIndexEnumType& bitIndex, T& registerValue);
00177
00186 T getBits(const BitMinion::BitIndexEnumType& bitIndex, const BitMinion::BitCountMaskEnumType& bitMask, const T& registerValue) const;
00187
00200 void setBits(const T& bitsValue, const BitMinion::BitIndexEnumType& bitIndex, const BitMinion::BitCountMaskEnumType& bitMask, T& registerValue);
00201 };
00202
00203 template <typename T>
00204 BitWizard<T>::BitWizard()
00205 {
00206 }
00207
00208 template <typename T>
00209 BitWizard<T>::~BitWizard()
00210 {
00211 }
00212
00213 template <typename T>
00214 inline bool BitWizard<T>::getBit(const BitMinion::BitIndexEnumType& bitIndex, const T& registerValue) const
00215 {
00216 return (((registerValue >> bitIndex) & BitMinion::ONE_BIT_MASK) == 1);
00217 }
00218
00219 template <typename T>
00220 inline bool BitWizard<T>::testBit(const bool& bitValue, const BitMinion::BitIndexEnumType& bitIndex, const T& registerValue) const
00221 {
00222 return (getBit(bitIndex, registerValue) == bitValue);
00223 }
00224
00225 template <typename T>
00226 inline void BitWizard<T>::setBit(const bool& bitValue, const BitMinion::BitIndexEnumType& bitIndex, T& registerValue)
00227 {
00228 registerValue = (registerValue & ~(1 << bitIndex)) | (bitValue << bitIndex);
00229 }
00230
00231 template <typename T>
00232 inline void BitWizard<T>::toggleBit(const BitMinion::BitIndexEnumType& bitIndex, T& registerValue)
00233 {
00234 registerValue ^= (1 << bitIndex);
00235 }
00236
00237 template <typename T>
00238 inline T BitWizard<T>::getBits(const BitMinion::BitIndexEnumType& bitIndex, const BitMinion::BitCountMaskEnumType& bitMask, const T& registerValue) const
00239 {
00240 return (registerValue >> bitIndex) & bitMask;
00241 }
00242
00243 template <typename T>
00244 inline void BitWizard<T>::setBits(const T& bitsValue, const BitMinion::BitIndexEnumType& bitIndex, const BitMinion::BitCountMaskEnumType& bitMask, T& registerValue)
00245 {
00246 registerValue = ((registerValue & ~(bitMask << bitIndex)) | ((bitsValue & bitMask) << bitIndex));
00247 }
00248
00249 #endif