BitField.hpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: BitField.hpp * 00006 * * 00007 * * 00008 * * 00009 * ITEC institute of the University of Klagenfurt (Austria) * 00010 * http://www.itec.uni-klu.ac.at * 00011 * * 00012 * * 00013 * For more information visit the ViTooKi homepage: * 00014 * http://ViTooKi.sourceforge.net * 00015 * vitooki-user@lists.sourceforge.net * 00016 * vitooki-devel@lists.sourceforge.net * 00017 * * 00018 * This file is part of ViTooKi, a free video toolkit. * 00019 * ViTooKi is free software; you can redistribute it and/or * 00020 * modify it under the terms of the GNU General Public License * 00021 * as published by the Free Software Foundation; either version 2 * 00022 * of the License, or (at your option) any later version. * 00023 * * 00024 * This program is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU General Public License * 00030 * along with this program; if not, write to the Free Software * 00031 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * 00032 * MA 02111-1307, USA. * 00033 * * 00034 ***********************************************************************/ 00035 00036 /*********************************************************************** 00037 * * 00038 * REVISION HISTORY: * 00039 * * 00040 * * 00041 * * 00042 ***********************************************************************/ 00043 00044 #ifndef PS_MPEG4ITEC_BITFIELD 00045 #define PS_MPEG4ITEC_BITFIELD 00046 #include"global.hpp" 00047 #ifdef ISOMP4 00048 #include "ISOMovies.h" 00049 #endif 00050 00062 class BitField { 00063 private: 00064 u32* bits; 00065 u32 maxSize; 00066 public: 00067 BitField(char* data, u32 size); 00068 BitField(u32 size, bool initValue); 00069 virtual ~BitField(); 00072 void setBit(u32 pos); 00075 void clearBit(u32 pos); 00078 bool getBit(u32 pos) const; 00079 00081 void invert(); 00082 00092 u32 getBitRange(u32 startPos, int* nrBits, bool littleEndian=true); 00093 00094 /* writes @param nrBits of @param value starting with @param startBit at position @param startPos. 00095 * returns false if startBit+nrBits is greater 32, if nrBits is not in range 1..32, 00096 * if startBit is not in range 0..32-nrBits, or startPos+nBits is greater than maxSize 00097 * @param littleEndian specifies endianness. When set to true the decimal value for the 00098 * requested bitrange is calculated as if the first bit has the lowest value. 00099 * e.g.: Bit 0 1000 Bit 3 -> littleEndian=true --> 1 littleEndian=false --> 8 00100 */ 00101 bool writeBitRange(u32 startPos, int nrBits, u32 value, u32 startBit, bool littleEndian=true); 00103 bool isAllSet() const; 00105 bool isAllCleared() const; 00107 float getPercentageSetBits() const; 00108 00118 BitField* createWithNewSize(u32 size, bool initValueForMissing); 00119 u32 getSize() const { return maxSize;} 00122 const char* toBinaryString(u32* sizeInU32) const; 00125 char* toAsciiString() const; 00131 char* toHexString() const; 00134 static BitField* fromAsciiString(const char* str); 00137 static BitField* fromBinaryString(const char* bin, u32 numBits); 00139 static BitField* fromBinaryString(char* bin, u32 numBits); 00140 static BitField* fromHexString(const char* bin); 00142 static void doTest(); 00143 }; 00144 #endif 00145 00146