C ++ primer Study Notes (3.5) standard library bitset type

Source: Internet
Author: User

3.5 standard library bitset type


Function: process the ordered set of binary bits.

# Include <bitset>

Using STD: bitset;

Method for initializing bitset variables:

Bitset <n> B;

B has N bits, each of which is 0

Bitset <n> B (U );

B is a copy of unsigned Long U.

Bitset <n> B (s );

B is a copy of the bits contained in String object S.

Bitset <n> B (S, POs, N );

B is the copy of N digits starting from position POs in S.

Note: N must be a constant expression.


For example:

Bitset <16> bitvec1 (0 xFFFF); // bits 0... 15 are set to 1

Bitset <32> bitvec2 (0 xFFFF); // bits 0... 15 are set to 1; 16... 31 are 0

The order in which the bitset is read from the string object is from right to left.

String strval ("1100 ");

Bitset <32> bitvec3 (strval );

The string object and the bitset object are converted in reverse order: the rightmost character of the string object (that is, the character with the largest subscript) it is used to initialize the low-level bits of a bitset object (that is, the bits of subscript 0 ). Remember this difference when initializing a bitset object with a string object.


Bitset object operations

B. Any ()

Is there a binary bit set to 1 in B?

B. None ()

Does B have a binary bit set to 1?

B. Count ()

Number of binary bits set to 1 in B

B. Size ()

Number of binary bits in B

B [POS]

Access the binary position at the POs in B

B. Test (POS)

Is the binary position at the POs in B 1?

B. Set ()

Set all binary bits in B to 1

B. Set (POS)

Set the binary position of B in POS to 1.

B. Reset ()

Set all binary bits in B to 0

B. Reset (POS)

Set the binary position of B in POS to 0.

B. Flip ()

Returns the inverse of all binary values in B.

B. Flip (POS)

Reverse the binary bits at the POs in B

B. to_ulong ()

Returns an unsigned long value with the same binary bits in B.

OS <B

Outputs the bitset in B to the OS stream.

Bitset <32> bitvec; // 32 bits, all zero

Bool is_set = bitvec. Any (); // false, all bits are zero

Bool is_not_set = bitvec. None (); // true, all bits are zero

Size_t bits_set = bitvec. Count (); // returns a size_t type bits_set value, counting the number of 1

The size_t type is a machine-related unsigned type with sufficient size.


You can use the following table operator to read and write data or test the binary position of an index:

For (INT Index = 0; index! = 32; index ++ ){

Bitvec [Index] = 0;

} // Use the following table operator to set all bits to zero


For (INT Index = 0; index! = 32; index ++ ){

Bitvec. Set (INDEX );

} // Use the set operation to set all bits to 1


If (bitvec. Test (I ))

// Test whether the I-th digit is 1


If (bitvec [I])

// Same as the previous function


Bitvec. Set (); // set all to one

Bitvec. Reset (); // set all to zero


Unsigned long ulong = bitvec. to_ulong ();

Cout <"ulong =" <ulong <Endl; // only available when the bitvec length is smaller than unsigned long


Output operation

# Include <iostream. h ># include <bitset> using namespace STD; int main () {bitset <32> bitvec (10); cout <"bitvec is" <bitvec <Endl; // output 00000000000000000000000000001010 // The binary value of decimal 10 is 1010, which fills up the bitvec 0th ~ 31-bit return 0 ;}

Note: The bits are numbered from right to left, and the rightmost is 0th !!


Exercise 3.24

// Initialize bitset <32> BV (0x20212e ); method 2: Use the number of digits in the cyclic function // The number of digits in the question to conform to the Fibonacci sequence. The sum of the first two values equals to the next bit <32> BV; int x = 0, y = 1, Z; # include <iostream. h ># include <bitset> using namespace STD; int main () {bitset <32> bitvec; int x = 0, y = 1, Z; Z = x + y; while (z <= 21) {bitvec. set (z); X = y; y = z; Z = x + y;} cout <"bitvec is" <bitvec <Endl; return 0 ;} // ouput: bitvec is 00000000001000000010000100101110

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.