The C++ standard library
In C++ programming, is part of the standard library; it provides a way to manipulate fixed-size bit sets.
A bit set is an array composed of bits, where each bit can be 0 or 1.
provides an efficient way to store and manipulate binary data, especially suitable for scenarios requiring bit-level operations, such as flag management and bitmask operations.
bitset is a template class whose template parameter defines the size of the bit set. For example,bitset<32> represents a bit set containing 32 bits.
Syntax
The following is how to use bitset 's basic syntax:
#include
// Declare a bitset of size N
std::bitset b;
// Initialize bitset
b = std::bitset(value);
// Access a single bit in the bit set
bool bit = b[i];
Basic usage of std::bitset
std::bitset is a template class used to represent a fixed-size sequence of binary bits. Its template parameter is the number of bits (N), representing the length of the binary sequence.
Defining std::bitset:
std::bitset<8> bits; // 定义一个 8 位的二进制序列
Initializing std::bitset:
-
Default initialization: all bits are
0. -
Initialize from an integer: converts the integer to binary.
-
Initialize from a string: parses the string into binary.
Example
std
::
bitset
<
8
bits1;
// Default initialization: 00000000
std
::
bitset
<
8
bits2
(
42
);
// Initialize from integer: 00101010
std
::
bitset
<
8
bits3
(
"10101010"
);
// Initialize from string: 10101010
Common member functions
std::bitset provides rich member functions to manipulate binary bits.
Accessing and modifying bits:
-
operator[]: Access or modify a specific bit. -
set(): Sets a specific bit or all bits to1. -
reset(): Sets a specific bit or all bits to0. -
flip(): Flips a specific bit or all bits.
Example
std
::
bitset
<
8
bits
(
"00001111"
);
bits
[
0
]
=
1
;
// Modify bit 0: 00001111 -> 00001111
bits.
set
(
4
)
;
// Set bit 4: 00001111 -> 00011111
bits.
reset
(
1
)
;
// Reset bit 1: 00011111 -> 00011101
bits.
flip
(
)
;
// Flip all bits: 00011101 -> 11100010
Querying bit information:
-
count(): Returns the number of1bits. -
size(): Returns the number of bits. -
test(pos): Checks whether a specific bit is1. -
all(): Checks whether all bits are1. -
any(): Checks whether any bit is1. -
none(): Checks whether all bits are0.
Example
std
::
bitset
<
8
bits
(
"10101010"
);
std
::
cout
<<
"Count of 1s: "
<<
bits.
count
(
)
<<
std
::
endl
;
// Output: 4
std
::
cout
<<
"Size: "
<<
bits.
size
(
)
<<
std
::
endl
;
// Output 8
std
::
cout
<<
"Is bit 3 set? "
<<
bits.
test
(
3
)
<<
std
::
endl
;
// Output 1 (true)
std
::
cout
<<
"All bits set? "
<<
bits.
all
(
)
<<
std
::
endl
;
// Output 0 (false)
Converting to other types:
-
to_ulong(): Setstd::bitsetConverts tounsigned long. -
to_ullong(): Setstd::bitsetConverts tounsigned long long. -
to_string(): Setstd::bitset; converts to a string.
Example
std
::
bitset
<
8
bits
(
"10101010"
);
unsigned
long
num
=
bits.
to_ulong
(
)
;
// Convert to integer: 170
std
::
string
str
=
bits.
to_string
(
)
;
// Convert to string: "10101010"
Bit operations
std::bitset supports common bit operations such as bitwise AND, bitwise OR, bitwise XOR, and bitwise NOT.
-
&: Bitwise AND -
|: Bitwise OR -
^: Bitwise XOR -
~: Bitwise NOT
Example
std
::
bitset
<
8
bits1
(
"10101010"
);
std
::
bitset
<
8
bits2
(
"11110000"
);
std
::
bitset
<
8
result_and
=
bits1
&
bits2;
// Bitwise AND: 10100000
std
::
bitset
<
8
result_or
=
bits1
|
bits2;
// Bitwise OR: 11111010
std
::
bitset
<
8
result_xor
=
bits1
^
bits2;
// Bitwise XOR: 01011010
std
::
bitset
<
8
result_not
=
~bits1;
// Bitwise NOT: 01010101
Example
Basic usage:
Example
#include
#include
int
main
(
)
{
std
::
bitset
<
8
b
(
"11001010"
);
// Initialize from string
std
::
cout
<<
"Initial bitset: "
<<
b
<<
std
::
endl
;
// Access a bit at a specific position
std
::
cout
<<
"Bit at position 3: "
<<
b
[
3
]
<<
std
::
endl
;
// Modify a bit
b
[
3
]
=
1
;
std
::
cout
<<
"Modified bitset: "
<<
b
<<
std
::
endl
;
// Flip a bit
b.
flip
(
)
;
std
::
cout
<<
"Flipped bitset: "
<<
b
<<
std
::
endl
;
return
0
;
}
Output result:
Initial bitset: 11001010
Bit at position 3: 0
Modified bitset: 11001011
Flipped bitset: 00110110
Bit operations:
Example
#include
#include
int
main
(
)
{
std
::
bitset
<
8
b1
(
"10101010"
);
std
::
bitset
<
8
b2
(
"11110000"
);
// Bitwise AND operation
std
::
bitset
<
8
b_and
=
b1
&
b2;
std
::
cout
<<
"Bitwise AND: "
<<
b_and
<<
std
::
endl
;
// Bitwise OR operation
std
::
bitset
<
8
b_or
=
b1
|
b2;
std
::
cout
<<
"Bitwise OR: "
<<
b_or
<<
std
::
endl
;
// Bitwise XOR operation
std
::
bitset
<
8
b_xor
=
b1
^
b2;
std
::
cout
<<
"Bitwise XOR: "
<<
b_xor
<<
std
::
endl
;
// Bitwise NOT operation
std
::
bitset
<
8
b_not
=
~b1;
std
::
cout
<<
"Bitwise NOT: "
<<
b_not
<<
std
::
endl
;
return
0
;
}
Output result:
Bitwise AND: 10100000
Bitwise OR: 11111010
Bitwise XOR: 01111010
Bitwise NOT: 01010101
Iterating over a bit set:
Example
#include
#include
int
main
(
)
{
std
::
bitset
<
8
b
(
"10101010"
);
// Iterate over bits in the bitset
for
(
size_t
i
=
0
;
i
<
b.
size
(
)
;
++
i
)
{
std
::
cout
<<
b
[
i
]
;
}
std
::
cout
<<
std
::
endl
;
return
0
;
}
Output result:
10101010
bitset is a very useful tool in the C++ standard library; it allows programmers to handle bit-level data in an intuitive and efficient way. Through the examples above, we can see how to declare, initialize, access, modify, and perform bit operations. These features are very useful when processing binary data or when bit-level control is needed.
Notes
-
std::bitset's size is fixed and determined at compile time. -
If the number of bits exceeds
unsigned longorunsigned long long's number of bits,to_ulong()andto_ullong()will throwstd::overflow_errorexception. -
std::bitsetdoes not support dynamic resizing; if you need a dynamic bit set, considerstd::vector.