Conjunto de bits
1. A bitset stores bits (elements with only two possible values: 0 or 1). No entanto, podemos obter a parte de uma string fornecendo posições ao construtor de bitset (as posições referem-se à posição da string da esquerda para a direita)
comando do windows arp
Exemplo:
C++// C++ program to demonstrate that we can get part of a // bit string in bitset. #include #include #include int main() { std::string bit_string = '110010'; std::bitset<8> b1(bit_string); // [0 0 1 1 0 0 1 0] // string from position 2 till end std::bitset<8> b2(bit_string 2); // [0 0 0 0 0 0 1 0] // string from position 2 till next 3 positions std::bitset<8> b3(bit_string 2 3); // [0 0 0 0 0 0 0 1] std::cout << b1 << 'n' << b2 << 'n' << b3 << 'n'; return 0; }
Saída:
00110010 00000010 00000001
2. We can construct a bitset using the characters in the std::basic_string _str. Uma posição inicial opcional _pos e comprimento _n podem ser fornecidos, bem como caracteres que denotam valores alternativos para bits definidos (_one) e não definidos (_zero).
Sintaxe:
std::bitset b1(str pos n zero one); str : string used to initialize the bitset pos : a starting offset into str n : number of characters to use from str zero : alternate character for unset bits in str one : alternate characters for set bits in str
- Se _pos > str.size() este construtor lança std::out_of_range.
// C++ program to demonstrate that we can construct bitset using // alternate characters for set and unset bits. #include #include #include int main() { // string constructor using custom zero/one digits std::string alpha_bit_string = 'aBaaBBaB'; std::bitset<8> b1(alpha_bit_string 0 alpha_bit_string.size() 'a' 'B'); // [01001101] std::cout << b1 << 'n'; }
Saída:
01001101
3. Constrói um objeto da classe bitset inicializando os N bits com valores que correspondem aos caracteres fornecidos em uma sequência de caracteres estilo C de zeros e uns. Você chama o construtor sem converter a string em um tipo de string. Ele também possui dois parâmetros opcionais _Zero e _One que indicam qual caractere em _Str deve ser interpretado como significando um bit 0 e um bit 1, respectivamente.
C++
#include #include int main() { // char* constructor using custom digits std::bitset<8> b1('XXXXYYYY' 8 'X' 'Y'); // [0 0 0 0 1 1 1 1] std::cout << b1 << 'n'; }
Saída:
00001111
Operações de bits
1.std::bitset::to_string()
Converte o conteúdo do bitset em uma string. Uses zero to represent bits with value of false and one to represent bits with value of true. A string resultante contém N caracteres com o primeiro caractere correspondendo ao último (N-1º) bit e o último caractere correspondendo ao primeiro bit. Also we can pass the characters used to print true and false value through the parameters.
Exemplo:
C++// C++ program to demonstrate that we can convert contents // of bitset to a string. #include #include int main() { std::bitset<8> b(42); std::cout << b.to_string() << 'n' << b.to_string('*') << 'n' << b.to_string('O' 'X') << 'n'; }
Saída:
00101010 **1*1*1* OOXOXOXO
2. stz::bitt.
Converts the contents of the bitset to an unsigned long integer. The first bit of the bitset corresponds to the least significant digit of the number and the last bit corresponds to the most significant digit. A função lança std::overflow_error se o valor não puder ser representado em unsigned long.
Exemplo:
C++// C++ program to demonstrate that we can get value of bitset // as unsigned long integer. #include #include int main() { std::bitset<5> b(5); std::cout << b.to_ulong() << 'n'; }
Saída:
5