[Boost] boost: dynamic_bitset Introduction

Source: Internet
Author: User

Bitset is similar to vector <bool>. It also stores binary bits, but its size is fixed and supports more bit operations than vector <bool>.
The advantages and disadvantages of vector <bool> and bitset are as follows: vector <bool> can increase dynamically, but cannot perform bit operations conveniently. bitset is the opposite, bitwise operations can be easily performed on Binary spaces, but cannot dynamically increase.
The appearance of boost. dynamic_bitset just fills the gap between the two. It is similar to the bitset of the standard library and provides a wide range of bitwise operations, and the length is dynamically changeable.
Dynamic_bitset is located in the namespace boost. To use the dynamic_bitset component, the header file <boost/dynamic_bitset.hpp> must be included, that is:
# Include <boost/dynamic_bitset.hpp>
Using namespace boost;

Void test_dynamic_bitset () {using namespace boost; // 1. construct dynamic_bitset <> db1; // empty dynamic_bitsetdynamic_bitset <> DB2 (10); // dynamic_bitsetdynamic_bitset <> db3 ('ox16', boost_binary (10101 )); // dynamic_bitset <> db4 (STD: string ("0101"); // string construction // 2. resizedb1.resize (8, true); Assert (db1.to _ ulong () = boost_binary (11111111); db1.resize (5); Assert (db1.to _ ulong () = boost_binary (11111); db1.clea R (); Assert (db1.empty () & db1.size () = 0); // 3. push_back // dynamic_bitset can use push_back () as a vector to append a value dynamic_bitset <> DB5 (5, boost_binary (01010) to the end of the container (Binary Number header )); assert (db5.to _ ulong () = boost_binary (01010); db5.push _ back (true); // Add a binary bit 1 assert (db5.to _ ulong () = boost_binary (101010); db5.push _ back (false); // Add a binary bit 0 assert (db5.to _ ulong () = boost_binary (0101010); // 4. block // dynamic_bitset use block to store Stores binary bits. A block can store 32 binary bits (assert (dynamic_bitset <> (32 ). num_blocks () = 1); Assert (dynamic_bitset <> (33 ). num_blocks () = 2); // 5. bit operation dynamic_bitset <> db6 (4, boost_binary (1010); db6 [0] & = 1; // bitwise operation and db6 [1] ^ = 1; // bitwise XOR operation db6 [2] | = 1; // bitwise OR operation assert (db6.to _ ulong () = boost_binary_ul (1100); // 6. access element dynamic_bitset <> db7 (4, boost_binary (1100); Assert (! Db7.test (0 )&&! Db7.test (1); Assert (db7.any ()&&! Db6.none (); Assert (db7.count () = 2); Assert (db7.set (). to_ulong () = boost_binary (1111); Assert (db7.reset (). to_ulong () == boost_binary (0000); Assert (db7.set (0, 1 ). to_ulong () = boost_binary (0001); Assert (db7.set (2, 1 ). to_ulong () === boost_binary (0101); Assert (db7.reset (0 ). to_ulong () = boost_binary (0100); Assert (db7.find _ First () = 2); Assert (db7.find _ next (2) = ulong_max ); // not found}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.