Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. C++ Dictionary
  3. bitset

bitset

std::bitset in the C++ standard library is a class for efficiently manipulating a fixed-length sequence of bits. It is useful whenever you want to bundle multiple on/off states together — such as flag management, permission control, or bitmask operations.

Syntax

#include <bitset>

// The template argument specifies the number of bits (must be a compile-time constant).
std::bitset<8> bits;            // 8 bits, all initialized to 0
std::bitset<8> bits2(0b10110); // initialized with a binary literal (C++14 and later)
std::bitset<8> bits3(42);      // initialized with an integer
std::bitset<8> bits4("10110"); // initialized with a string of '0'/'1' characters

// Reading and writing individual bits
bits.set(3);         // set bit 3 to 1
bits.reset(3);       // set bit 3 to 0
bits.flip(3);        // toggle bit 3
bool b  = bits[3];   // read bit 3 (no range check)
bool b2 = bits.test(3);  // read bit 3 (throws on out-of-range)

// Operations on the entire bit sequence
bits.set();   // set all bits to 1
bits.reset(); // set all bits to 0
bits.flip();  // toggle all bits

// Information queries
bits.count();  // number of bits set to 1
bits.size();   // total number of bits
bits.any();    // true if at least one bit is 1
bits.none();   // true if all bits are 0
bits.all();    // true if all bits are 1

// Bitwise operators (operands must have the same size)
auto r1 = bits & bits2;  // AND
auto r2 = bits | bits2;  // OR
auto r3 = bits ^ bits2;  // XOR
auto r4 = ~bits;         // NOT
auto r5 = bits << 2;     // left shift
auto r6 = bits >> 2;     // right shift

// Conversions
unsigned long ul = bits.to_ulong();   // convert to unsigned long
std::string  str = bits.to_string();  // convert to "01010110" string

Member Functions and Operators

Member Function / OperatorDescription
set(pos)Sets the bit at the specified position to 1. With no argument, sets all bits to 1.
reset(pos)Sets the bit at the specified position to 0. With no argument, sets all bits to 0.
flip(pos)Toggles the bit at the specified position. With no argument, toggles all bits.
test(pos)Returns the value of the bit at the specified position as bool. Throws std::out_of_range if out of bounds.
operator[]Reads or writes the bit at the specified position. No range checking.
count()Returns the number of bits that are set to 1.
size()Returns the total length of the bit sequence.
any()Returns true if at least one bit is 1.
none()Returns true if all bits are 0.
all()Returns true if all bits are 1.
&, |, ^, ~Bitwise AND, OR, XOR, NOT operations. Both operands must have the same size.
<<, >>Shifts the bit sequence left or right.
to_ulong()Converts the bit sequence to unsigned long. Throws if the value overflows.
to_ullong()Converts the bit sequence to unsigned long long (C++11 and later).
to_string()Converts the bit sequence to a std::string in "01..." format.

Sample Code

jujutsu_bitset.cpp
#include <iostream>
#include <bitset>
#include <string>

// Bit positions representing cursed technique flags
const int FLAG_CURSED_ENERGY    = 0;  // basic cursed energy control
const int FLAG_DIVERGENT_FIST   = 1;  // Divergent Fist (Itadori's technique)
const int FLAG_BLACK_FLASH      = 2;  // Black Flash attack
const int FLAG_SHADOW_TECHNIQUE = 3;  // Ten Shadows Technique (Fushiguro's)
const int FLAG_STRAW_DOLL       = 4;  // Straw Doll Technique (Kugisaki's)
const int FLAG_SIX_EYES         = 5;  // Six Eyes (Gojo's special ability)
const int FLAG_LIMITLESS        = 6;  // Limitless (Gojo's technique)
const int FLAG_RATIO_TECHNIQUE  = 7;  // Ratio Technique (Nanami's technique)

const std::string FLAG_NAMES[] = {
    "Cursed Energy",
    "Divergent Fist",
    "Black Flash",
    "Ten Shadows Technique",
    "Straw Doll Technique",
    "Six Eyes",
    "Limitless",
    "Ratio Technique",
};

void printFlags(const std::string& name, const std::bitset<8>& flags) {
    std::cout << "[" << name << "]" << std::endl;
    std::cout << "  Bit string: " << flags.to_string() << std::endl;
    std::cout << "  Flag count: " << flags.count() << std::endl;
    std::cout << "  Techniques:" << std::endl;
    for (int i = 0; i < 8; i++) {
        if (flags.test(i)) {
            std::cout << "    - " << FLAG_NAMES[i] << std::endl;
        }
    }
    std::cout << std::endl;
}

int main() {
    // Itadori Yuji: Cursed Energy, Divergent Fist, Black Flash
    std::bitset<8> itadori;
    itadori.set(FLAG_CURSED_ENERGY);
    itadori.set(FLAG_DIVERGENT_FIST);
    itadori.set(FLAG_BLACK_FLASH);

    // Fushiguro Megumi: Cursed Energy, Ten Shadows Technique
    std::bitset<8> fushiguro;
    fushiguro.set(FLAG_CURSED_ENERGY);
    fushiguro.set(FLAG_SHADOW_TECHNIQUE);

    // Kugisaki Nobara: Cursed Energy, Straw Doll Technique
    std::bitset<8> kugisaki;
    kugisaki.set(FLAG_CURSED_ENERGY);
    kugisaki.set(FLAG_STRAW_DOLL);

    // Gojo Satoru: Cursed Energy, Six Eyes, Limitless
    std::bitset<8> gojo;
    gojo.set(FLAG_CURSED_ENERGY);
    gojo.set(FLAG_SIX_EYES);
    gojo.set(FLAG_LIMITLESS);

    // Okkotsu Yuta: Cursed Energy, Black Flash, Ratio Technique
    std::bitset<8> okkotsu;
    okkotsu.set(FLAG_CURSED_ENERGY);
    okkotsu.set(FLAG_BLACK_FLASH);
    okkotsu.set(FLAG_RATIO_TECHNIQUE);

    std::cout << "=== Sorcerer Technique Flags ===" << std::endl << std::endl;
    printFlags("Itadori Yuji",    itadori);
    printFlags("Fushiguro Megumi",fushiguro);
    printFlags("Kugisaki Nobara", kugisaki);
    printFlags("Gojo Satoru",     gojo);
    printFlags("Okkotsu Yuta",    okkotsu);

    // AND: common flags between Itadori and Okkotsu
    std::cout << "=== Common flags: Itadori & Okkotsu (AND) ===" << std::endl;
    std::bitset<8> common = itadori & okkotsu;
    std::cout << "  Bit string: " << common.to_string() << std::endl;
    for (int i = 0; i < 8; i++) {
        if (common.test(i)) std::cout << "    - " << FLAG_NAMES[i] << std::endl;
    }
    std::cout << std::endl;

    // OR: all flags held by any character
    std::cout << "=== All flags combined (OR) ===" << std::endl;
    std::bitset<8> allFlags = itadori | fushiguro | kugisaki | gojo | okkotsu;
    std::cout << "  Bit string: " << allFlags.to_string() << std::endl;
    std::cout << "  Unique technique count: " << allFlags.count() << std::endl << std::endl;

    // XOR: flags held by only one of Itadori and Fushiguro
    std::cout << "=== Different flags: Itadori vs Fushiguro (XOR) ===" << std::endl;
    std::bitset<8> xorResult = itadori ^ fushiguro;
    std::cout << "  Bit string: " << xorResult.to_string() << std::endl;
    for (int i = 0; i < 8; i++) {
        if (xorResult.test(i)) std::cout << "    - " << FLAG_NAMES[i] << std::endl;
    }
    std::cout << std::endl;

    // Integer representation
    std::cout << "=== Integer representation ===" << std::endl;
    std::cout << "  Itadori flag value (decimal): " << itadori.to_ulong() << std::endl;
    std::cout << "  Gojo flag value (decimal):    " << gojo.to_ulong()    << std::endl;

    return 0;
}
g++ -std=c++11 jujutsu_bitset.cpp -o jujutsu_bitset
./jujutsu_bitset
=== Sorcerer Technique Flags ===

[Itadori Yuji]
  Bit string: 00000111
  Flag count: 3
  Techniques:
    - Cursed Energy
    - Divergent Fist
    - Black Flash

[Fushiguro Megumi]
  Bit string: 00001001
  Flag count: 2
  Techniques:
    - Cursed Energy
    - Ten Shadows Technique

[Kugisaki Nobara]
  Bit string: 00010001
  Flag count: 2
  Techniques:
    - Cursed Energy
    - Straw Doll Technique

[Gojo Satoru]
  Bit string: 01100001
  Flag count: 3
  Techniques:
    - Cursed Energy
    - Six Eyes
    - Limitless

[Okkotsu Yuta]
  Bit string: 10000101
  Flag count: 3
  Techniques:
    - Cursed Energy
    - Black Flash
    - Ratio Technique

=== Common flags: Itadori & Okkotsu (AND) ===
  Bit string: 00000101
    - Cursed Energy
    - Black Flash

=== All flags combined (OR) ===
  Bit string: 11111111
  Unique technique count: 8

=== Different flags: Itadori vs Fushiguro (XOR) ===
  Bit string: 00001110
    - Divergent Fist
    - Black Flash
    - Ten Shadows Technique

=== Integer representation ===
  Itadori flag value (decimal): 7
  Gojo flag value (decimal):    97

Common Mistake 1: Bit position indexing confusion

In std::bitset, bit position 0 is the least significant bit (rightmost). When initializing from a string, the leftmost character is the most significant bit. The index and visual position can be confusing.

// NG: expecting flags[0] to correspond to the leftmost character of "10000000"
std::bitset<8> flags("10000000");
std::cout << flags[0] << std::endl;  // prints 0, not 1

OK: Access the most significant bit via index 7.

// OK: bit 7 is the most significant bit (leftmost in string representation)
std::bitset<8> flags("10000000");
std::cout << flags[7] << std::endl;       // 1
std::cout << flags.to_string() << std::endl; // "10000000"

Common Mistake 2: Overflow with to_ulong()

If the bit count exceeds the width of unsigned long, to_ulong() throws std::overflow_error. For large bitsets, use to_string() instead.

// Caution: to_ulong()/to_ullong() may throw for bitsets wider than 64 bits
std::bitset<128> bigFlags;
bigFlags.set(100);
// bigFlags.to_ullong();  // throws std::overflow_error

// OK: to_string() works for any bit count
std::string bitStr = bigFlags.to_string();

Overview

std::bitset fixes its bit count at compile time via a template argument, so it cannot be resized at runtime. When a variable size is needed, consider std::vector<bool> or boost::dynamic_bitset. In exchange for the fixed size, it stores bits compactly (8 bits per byte) and many implementations map operations like count() to hardware instructions (such as POPCNT), making them fast. It is commonly used for flag management, permission control, and game state tracking. The to_ulong() and to_string() methods make conversion to and from other types straightforward. See also the Operators page for details on bitwise operators.

If you find any errors or copyright issues, please .