UtilityToolsLab

© 2026 UtilityToolsLab. Built and maintained by the UtilityToolsLab Team.

Free eBooks·About·Changelog·Privacy Policy·Terms of Service·Report a bug
HomeCompetitive ProgrammingBitmask Planner

Related Tools

Code FormatterMatrix GeneratorComplexity CalcBit VisualizerPrime FactorsMEX CalcInterval MergerMatrix RotationPBDS GeneratorSegment TreeGraph VisualizerStress TesterModulo CalcConvex HullPath FinderOffline JudgeBig-O AnalyzerCombinatoricsDP Table BuilderExtended GCDSieve VisualizerBinary SearchSorting VisualizerSparse Table RMQUnion-Find DSU

Bitmask & Subset DP Planner

Input set size N (≤12). Visualize all 2^N bitmasks, submask iterations, and generate ready-to-paste C++ DP skeleton.

You Might Also Like

All Competitive Programming

Bit Visualizer

Toggle 32/64-bit grids. Click bits to flip them live and see decimal recalculate. Shows popcountll, clzll, ctzll, and MSB instantly.

Code Formatter

Format and beautify C++, Java, and Python code with consistent indentation. 100% client-side — no code leaves your browser.

Matrix Generator

Generate grid/matrix inputs for competitive programming. Random, zeros, identity, or sequential fill. Outputs in multiple formats.

Complexity Calc

Estimate Big-O operations and runtime for any N. Interactive reference table for all complexity classes from O(1) to O(N!).

Subset DP is one loop and one line of bit arithmetic, and the line is s = (s - 1) & mask. It enumerates every submask of a mask in descending order without ever visiting one that is not a submask, and it is far from obvious why. The Bitmask Planner shows the output of that loop for any mask you click, alongside a table of every mask for a set of size N.

Two counters sit at the top and they are the numbers that decide whether your approach fits. The total mask count is 2 to the N. The total work of iterating submasks across every mask is 3 to the N, not 4 to the N, because each element is in the mask, in the submask, or in neither.

A C++ skeleton at the foot updates with your chosen N, complete with the __builtin_popcount call and the submask loop commented in place. Copy it and the button confirms with Got it! Copied 🚀.

Walkthrough: Submasks of 13

Set N to 4 and click the row for mask 13, binary 1101. The panel lists 7 submasks in the order the loop produces them: 13, 12, 9, 8, 5, 4, 1. Note that 13 is included, since a mask is a submask of itself, and 0 is excluded, since the loop condition stops at zero. Seven is exactly 2 to the power of the popcount minus 1, and popcount of 13 is 3.

How Big 2ⁿ and 3ⁿ Get

  • N is capped at 12 here, giving 4,096 masks and 531,441 submask steps. That is a browser limit, not an algorithmic one, and it exists because the table renders a row per mask.
  • Only the first 32 rows are drawn initially, with a Show all link underneath. At N of 12 that link expands the table by a factor of 128.
  • In real problems the practical ceiling is around N of 20 for 2ⁿ work, roughly a million masks, and around N of 16 for full 3ⁿ submask enumeration, roughly 43 million steps.
  • Mask 0 has no submasks at all under this loop and the panel says so directly with No proper submasks (mask = 0). Handle that case explicitly in your own code.
  • Selecting a row a second time deselects it, and changing N clears the selection, because a mask index means something different at a different set size.
3

Total masks

8

Submask iterations across all masks (3n)

27

All Bitmasks

Click a mask to see its submasks

MaskBinarySet Bits|S|
0000{}0
1001{0}1
2010{1}1
3011{0, 1}2
4100{2}1
5101{0, 2}2
6110{1, 2}2
7111{0, 1, 2}3

C++ Bitmask DP Template

// Bitmask DP skeleton for N = 3
// Total masks: 8
#include <bits/stdc++.h>
using namespace std;

const int N = 3;
int dp[1 << N];

void solve() {
    // Iterate over all masks
    for (int mask = 0; mask < (1 << N); mask++) {
        // __builtin_popcount(mask) = number of set bits
        int bits = __builtin_popcount(mask);
        
        // Submask iteration for mask = m
        // for (int s = mask; s > 0; s = (s-1) & mask) { ... }
        
        // Example: dp transition
        dp[mask] = 0; // TODO: fill your logic
    }
}

int main() {
    int n; cin >> n;
    solve();
}