In C++bitset
is 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>
。 -
statement:
bitset<N>
Indicates a containingN
Binary set of bits (N
is a compile-time constant).
bitset<8> b1; // Default initialization, all bits are 0bitset<4> b2(5); // Initialize with integers: 0101bitset<4> b3("1010"); // Initialize with string: 1010bitset<4> b4(b3); // Copy construction
2. Initialization rules
- Integer initialization: High truncation, low truncation.
bitset<3> 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.
- Strings can only contain
bitset<4> b("1010"); // High → Low : 1 0 1 0 → Decimal 10bitset<4> 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<4> 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 << "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 <bitset> #include <iostream> using namespace std; int main() { bitset<8> b1(42); // 00101010 bitset<8> b2("10101010"); // 10101010 cout << b1 << endl; // Output 00101010 (0); // becomes 00101011 (); // becomes 0101010101 cout << (b1 & b2) << endl; // 00000001 cout << b1.to_ulong() << endl; // 43 return 0; }
Things to note
-
Visit across the border:
test(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.
bitset
It 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!