C++ bitset zaujímavé fakty
Bitset je kontajner v C++ Standard Template Library pre prácu s dátami na bitovej úrovni.
1. A bitset stores bits (elements with only two possible values: 0 or 1). Časť reťazca však môžeme získať poskytnutím pozícií konštruktorovi bitovej množiny (pozície sú vzhľadom na pozíciu reťazca zľava doprava)
Príklad:
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 ; }
výstup:
00110010 00000010 00000001
2. Môžeme zostaviť bitovú množinu pomocou znakov v std::basic_string _str. Môže byť poskytnutá voliteľná počiatočná pozícia _pos a dĺžka _n, ako aj znaky označujúce alternatívne hodnoty pre nastavený (_one) a nenastavený (_nula) bitov.
Syntax:
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
- Ak _pos > str.size(), tento konštruktor vyhodí std::out_of_range.
- Ak niektorý zo znakov skúmaných v _str nie je nula alebo jedna, vyhodí std::invalid_argument.
// 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' ; }
výstup:
01001101
3. Skonštruuje objekt bitovej množiny triedy, ktorý inicializuje N bitov na hodnoty, ktoré zodpovedajú znakom poskytnutým v reťazci znakov v štýle c s nulami a jednotkami. Zavoláte konštruktor bez pretypovania reťazca do typu reťazca. Má tiež dva voliteľné parametre _Zero a _One, ktoré označujú, ktorý znak v _Str sa má interpretovať ako 0 bit a 1 bit.
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' ; }
výstup:
00001111
Bitset Operations
1. std::bitset::to_string()
Skonvertuje obsah bitovej sady na reťazec. Používa nulu na reprezentáciu bitov s hodnotou nepravda a jedna na reprezentáciu bitov s hodnotou true. Výsledný reťazec obsahuje N znakov, pričom prvý znak zodpovedá poslednému (N-1.) bitu a posledný znak zodpovedá prvému bitu. Also we can pass the characters used to print true and false value through the parameters.
Príklad:
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' ; }
výstup:
00101010 **1*1*1* OOXOXOXO
2. stz::bitt.
Skonvertuje obsah bitovej množiny na dlhé celé číslo bez znamienka. Prvý bit bitovej sady zodpovedá najmenej platnej číslici čísla a posledný bit zodpovedá najvýznamnejšej číslici. Funkcia vyvolá std::overflow_error, ak hodnotu nemožno reprezentovať v unsigned long.
Príklad:
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' ; }
výstup:
5