但很不巧的是,有時就是偏偏會碰上,更不巧的是.因為很少碰.所以也就不知道該怎麼寫處理bit的程式.
其實在C++是使用bitset這個class
bitset的操作行為:
any | true if any bits are set |
count | returns the number of set bits |
flip | reverses the bitset |
none | true if no bits are set |
reset | sets bits to zero |
set | sets bits |
size | number of bits that the bitset can hold |
test | returns the value of a given bit |
to_string | string representation of the bitset |
to_ulong | returns an integer representation of the bitset |
範例1 (開啟某個bit):
#include
#include
using namespace std;
int main ()
{
bitset<4> mybits;
cout << class="comm">// 1111
cout << class="comm">// 1011
cout << class="comm">// 1111
return 0;
}
範例2:
#include
#include
#include
using namespace std;
int main (關閉某個bit)
{
bitset<4> mybits (string("1011"));
cout << class="comm">// 1001
cout << class="comm">// 0000
return 0;
}