SoFunction
Updated on 2025-04-08

Detailed explanation of common usage examples of bitset in c++

In C++bitsetis a template class for processing fixed-size bit sequences, providing efficient bit operation functions. Here is a detailed introduction to its key features:

1. Declaration and Initialization

  • Header file: Need to include<bitset>
  • statementbitset<N>Indicates a containingNBinary set of bits (Nis a compile-time constant).
bitset&lt;8&gt; b1;       // Default initialization, all bits are 0bitset&lt;4&gt; b2(5);    // Initialize with integers: 0101bitset&lt;4&gt; b3("1010"); // Initialize with string: 1010bitset&lt;4&gt; b4(b3);   // Copy construction

2. Initialization rules

  • Integer initialization: High truncation, low truncation.
bitset&lt;3&gt; b(10);    // 10The binary system is1010 → Truncate to010(The value is2)
  • String initialization
    • Strings can only contain'0'and'1'
    • By default, from stringLeft end (high position)Start parsing.
bitset&lt;4&gt; b("1010"); // High → Low : 1 0 1 0 → Decimal 10bitset&lt;4&gt; b("101010", 2, 4); // From index2Pick4Bit:"1010" → The value is10

3. Common operations

  • bit access

    • []Operator: Accessed through index (from right to left, 0 is the lowest bit).
    • test(pos): Check whether the specified bit is 1, and an exception is thrown out of bounds.
bitset&lt;4&gt; b("1010");
bool bit0 = b[0];   // 0 → lowest positionbool bit3 = b[3];   // 1 → Highest position
  • Modify bit

    • set(pos, val): Set a certain bit (default 1).
    • reset(pos): Clear a bit (set to 0).
    • flip(pos): Flip a certain position.
(0);       // 1010 → 1011
(3);     // 1011 → 0011
(1);      // 0011 → 0001
  • Statistics and judgments

    • count(): Returns the number of 1.
    • any()/none()/all(): Determine whether there is/no/all are 1.
if (()) cout &lt;&lt; "At least 1 bit is 1";

4. Bit operation

  • support&|^~<<>>Equal operator (the length must be the same):
bitset<4> a("1010"), b("1100");
auto c = a & b;     // 1000
auto d = a << 1;    // 0100

5. Type conversion

  • to_ulong()/to_ullong(): Convert to an unsigned integer (possibly overflowed).
  • to_string(): Convert to a string to specify padding characters.
bitset<4> b(10);
string s = b.to_string(); // "1010"

6. Application scenarios

  • Bit flag management: such as permission control, status marking.
  • Bitmask operation: Quickly filter or modify specific bits.
  • Data compression: efficiently store boolean values.

Sample code

#include &lt;bitset&gt;
#include &lt;iostream&gt;
using namespace std;
int main() {
    bitset&lt;8&gt; b1(42);          // 00101010
    bitset&lt;8&gt; b2("10101010");  // 10101010
    cout &lt;&lt; b1 &lt;&lt; endl;         // Output 00101010    (0);                 // becomes 00101011    ();                 // becomes 0101010101    cout &lt;&lt; (b1 &amp; b2) &lt;&lt; endl; // 00000001
    cout &lt;&lt; b1.to_ulong() &lt;&lt; endl; // 43
    return 0;
}

Things to note

  • Visit across the bordertest(pos)Will check for cross-border,[]Won't.
  • performance:Efficient bit operation, suitable for intensive computing.
  • String order: The first character of the string corresponds to the high bit.

bitsetIt is a powerful tool for processing binary data. Combined with clear bit sequence rules and rich interfaces, it can significantly simplify bit-level operation code.

This is the end of this article about the common usage of bitset in c++. For more related content on the usage of bitset, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!