同样也不说废话了,直接贴代码:
/* * CCBitset.h * c++_common_codes * * Created by xichen on 12-1-13. * Copyright 2012 cc_team. All rights reserved. * */ #ifndef CC_BITSET_H #define CC_BITSET_H #include "ccVector.h" #include <ostream> template <size_t bitLen> class CCBitset { public: CCBitset(unsigned long n = 0); ~CCBitset() { } public: CCBitset(const CCBitset & bitset); CCBitset & operator=(const CCBitset & bitset); public: bool any() const; bool none() const; unsigned long count() const; unsigned long size() const; unsigned long length() const; bool operator[](size_t pos) const; bool test(size_t pos) const; void set(); void set(size_t pos); void reset(); void reset(size_t pos); void flip(); void flip(size_t pos); unsigned long to_ulong() const; public: // friend std::ostream & operator<<(std::ostream & os, const CCBitset & bitset); private: size_t _len; size_t _len_1; CCVector<bool> _data; }; template <size_t bitLen> unsigned long CCBitset<bitLen>::to_ulong() const { unsigned long n = 0L; for (int i = _len - 1; i >= 0; --i) n = n * 2 + _data[i]; return n; } template <size_t bitLen> void CCBitset<bitLen>::flip( size_t pos ) { if(_data[pos]) _data[pos] = 0, --_len_1; else _data[pos] = 1, ++_len_1; } template <size_t bitLen> void CCBitset<bitLen>::flip() { _len_1 = 0; for (int i = 0; i < _len; ++i) { if(_data[i]) _data[i] = 0; else _data[i] = 1, ++_len_1; } } template <size_t bitLen> void CCBitset<bitLen>::reset( size_t pos ) { if(_data[pos]) _data[pos] = 0, --_len_1; } template <size_t bitLen> void CCBitset<bitLen>::reset() { memset(_data.data(), 0, _len); _len_1 = 0; } template <size_t bitLen> void CCBitset<bitLen>::set( size_t pos ) { if(!_data[pos]) _data[pos] = 1, ++_len_1; } template <size_t bitLen> void CCBitset<bitLen>::set() { memset(_data.data(), 1, _len); _len_1 = _len; } template <size_t bitLen> bool CCBitset<bitLen>::test( size_t pos ) const { return _data[pos]; } template <size_t bitLen> bool CCBitset<bitLen>::operator[]( size_t pos ) const { return _data[pos]; } template <size_t bitLen> unsigned long CCBitset<bitLen>::length() const { return _len; } template <size_t bitLen> unsigned long CCBitset<bitLen>::size() const { return _len; } template <size_t bitLen> unsigned long CCBitset<bitLen>::count() const { return _len_1; } template <size_t bitLen> bool CCBitset<bitLen>::none() const { return _len_1 == 0; } template <size_t bitLen> bool CCBitset<bitLen>::any() const { return _len_1 > 0; } template <size_t bitLen> CCBitset<bitLen> & CCBitset<bitLen>::operator=( const CCBitset<bitLen> & bitset ) { if(*this == bitset) return *this; _len = bitset._len; _len_1 = bitset._len_1; _data = bitset._data; return *this; } template <size_t bitLen> CCBitset<bitLen>::CCBitset( const CCBitset & bitset ) { _len = bitset._len; _len_1 = bitset._len_1; _data = bitset._data; } template <size_t bitLen> CCBitset<bitLen>::CCBitset( unsigned long n) { _len = bitLen; _len_1 = 0; for(int i = 0; i < bitLen; ++i) { bool j = n % 2; _data.push_back(j); n /= 2; if(j == true) ++_len_1; } } #endif
简单的测试代码:
void ccTestCCBitset() { #if 1 // CCBitset CCBitset<4> bs(17); COUT_ENDL(bs.any()) COUT_ENDL(bs.count()) COUT_ENDL(bs.to_ulong()) bs.flip(); // COUT_ENDL(bs) COUT_ENDL(bs.to_ulong()) COUT_ENDL(bs.count()) COUT_ENDL(bs.length()) COUT_ENDL(bs.none()) COUT_ENDL(bs.size()) COUT_ENDL(bs.test(1)) bs.reset(); // COUT_ENDL(bs) COUT_ENDL(bs.count()) COUT_ENDL(bs.to_ulong()) #endif }
测试结果:
1 1 1 14 3 4 0 4 1 0 0
注:
1、CCVector定义见:http://blog.csdn.net/cxsjabcabc/article/details/7302423
2、COUT_ENDL宏为:
#define COUT_ENDL(message) std::cout << (message) << std::endl;