C + + standard library Bitset type (simple use method)

Source: Internet
Author: User
Tags bit set bitset

Turn from this person's blog

#include <bister>

Using Std::bitset;

A word definition: A customizable number of bits that can be used as the data type of a binary record.

One, define and initialize

Bitset<n> b; B has n bits, each of which is 0;

Bitset<n> b (U); B is a copy of the unsigned long type U

Bitset<n> b (s); B is a copy of the string object s that contains n-bit strings

Bitset<n> B (S, POS, n); B is a copy of n locations in s that start at the POS position

Bitset<n> b (S,pos); B starts from the POS position of S to the end of S (the value of the note starts at the right end of B)

Note: The number of bits defined by ①n is populated with initial values at initialization, the range of assignments exceeds, and the spare is filled with 0.

②bitset reads a bit set from a string object in a right-to-left order.

Two, operation

B.any (); Does finding B exist in 1?

B.none (); There is no 1 in B?

B.count (); Number of 1 in B

B.size (); The number of bits in B

B[pos]; Accessing the value at POS in B

B.test (POS); Detects if POS is 1 in b

B.set (); Put all positions in B at 1

B.set (POS); Put the POS position in B to 1

B.reset (); Put all positions in B at 0

B.reset (POS); Put the POS position in B to 0

B.flip (); All bits in B are reversed

B.flip (POS); B in the POS at the bits counter

B.to_ulong; Returns a unsigned Long value

OS << b; Put the B-bit set output

C + + standard library Bitset type (simple use method)

Related Article

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.