Member Function | Description |
all | It returns true when all bits are 1. It returns false, if one or more bits are 0. |
any | It returns true when at least one bit is 1. It returns false, if all the bits are 0. |
count | It returns the total number of bits set to 1. |
flip | It toggles the value of all the bits, or it toggles a single bit at the specified position. |
none | It returns true when all bits are 0. It returns false, if one or more bits are 1. |
reset | It sets the value of all the bits to 0 or at the specified position. |
set | It sets the value of all the bits to 1 or at the specified position. |
size | It returns the number of bits. |
test | It tests whether the bit at a specified position is o 1. |
to_ullong | It returns the sum of the bit values as an unsigned long long. |
to_ulong | It returns an unsigned long that would generate the sequence of bits contained if used to initialize the bitset. |
to_string | It returns a string representing the bits. |
Program1.cpp |
#include <bitset> std::bitset<4> data; data[0] = 1; data[1] = 0; data[2] = 1; data[3] = 1; |
Program2.cpp |
#include <bitset> std::bitset<4> data(11); wstring text; text += data[0] ? L"1" : L"0"; text += data[1] ? L"1" : L"0"; text += data[2] ? L"1" : L"0"; text += data[3] ? L"1" : L"0"; // the value of text is number eleven in binary: L"1011" |
Program3.cpp |
#include <bitset> std::bitset<4> data(wstring(L"0110")); wstring text; text += data[0] ? L"1" : L"0"; text += data[1] ? L"1" : L"0"; text += data[2] ? L"1" : L"0"; text += data[3] ? L"1" : L"0"; // the value of text is: L"0110" |
Sys::BoolArray |
Wintempla provides the Sys::BoolArray class to efficently store an array of bools, the size of the array can be set at run time and can handle bigger arrays than bitset . Wintempa proporciona la clase Sys::BoolArray para almacenar en forma eficiente un arreglo de bools, el tamaño del arreglo se puede fijar en el momento de ejecución y puede manejar arreglos más grandes que bitset. |
Program1.cpp |
Sys::BoolArray data(10); for (size_t i = 0; i < data.GetBitCount(); i++) { data.SetBit(i, i%2 == 0); } data.Show(hWnd, L"Value"); |
Program2.cpp |
Sys::BoolArray data(18); for (size_t i = 0; i < data.GetBitCount(); i++) { data.SetBit(i, i%3 == 0); } wstring text; data.GetString(text); ::MessageBox(NULL, text.c_str(), L"BoolArray", MB_OK); |
Program3.cpp |
Sys::BoolArray data(L"000011110001"); data.Show(hWnd, L"1"); const int count = data.GetActiveBitCount(); wchar_t text[32]; _snwprintf_s(text, 32, _TRUNCATE, L"Number of Ones = %d", count); ::MessageBox(NULL, text, L"BoolArray", MB_OK); data.SetAllToOne(); data.Show(hWnd, L"2"); data.SetAllToZero(); data.Show(hWnd, L"3"); data.FlipBit(3); data.Show(hWnd, L"4"); |
Program4.cpp |
Sys::BoolArray data(24); data.SetInt8(0, 120); data.SetInt8(1, 92); data.SetInt8(2, -40); wchar_t text[64]; _snwprintf_s(text, 64, _TRUNCATE, L"%d, %d, %d", data.GetInt8(0), data.GetInt8(1), data.GetInt8(2)); ::MessageBox(NULL, text, L"BoolArray", MB_OK); |
Program5.cpp |
Sys::BoolArray a(L"1011101110011"); Sys::BoolArray b(16); b.CopyBits(a, 2, 8); b.Show(hWnd, L"b"); |