In C++bitset
It is a template class for handling 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 0 bitset<4> b2(5); // Initialize with integer: 0101 bitset<4> b3("1010"); // Initialize with string: 1010 bitset<4> b4(b3); // Copy construct
2. Initialization rules
-
Integral initialization: High truncation, low truncation.
bitset<3> b(10); // The binary of 10 is 1010 → truncated to 010 (value is 2)
-
String initialization:
- Strings can only contain
'0'
and'1'
。 - By default, from stringLeft end (high position)Start parsing.
bitset<4> b("1010"); // High → Low : 1 0 1 0 → Decimal 10 bitset<4> b("101010", 2, 4); // Take 4 bits from index 2: "1010" → The value is 10
- Strings can only contain
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 bit bool bit3 = b[3]; // 1 → highest bit
-
-
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); // 0010101010
bitset<8> b2("10101010"); // 10101010
cout << b1 << endl; // Output 0010101010
(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.