本文へスキップ
製品デスクトップツールTASTECAD図面の確認TASTEFILESファイル管理TASTEFTPファイル転送TASTESSHリモートワークスペースTASTEZIP圧縮と展開TASTEWINMファイル管理開発ツールTASTECODERust 統合開発環境TASTESTUDIO統合開発・テスト環境TASTEETLデータ統合(ETL)サーバーTASTEREMOTEリモートデスクトップサーバーTASTEGEO空間情報サーバーTASTEMAILメールワークスペース投資TASTESTOCK株式自動売買デスクトップアプリはじめるダウンロードドキュメント購入すべての製品
マーケットプレイス
ニュース
技術ニュース今日の開発ニュース開発ドキュメントプログラミング言語RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDartWebフロントエンド フレームワークNext.jsReactVue.js · NuxtSvelteKitAngularサーバー フレームワークSpring BootDjangoFastAPIASP.NET CoreNestJS · Expressアプリ・デスクトップ フレームワークTauriFlutterReact NativeElectronアルゴリズムアルゴリズムの基礎主要アルゴリズム発展アルゴリズム実務のアルゴリズム
コミュニティプロジェクト開発ログプロンプトAIツールの問題ショーケースコミュニティガイド
協業技術移転ウェブサイト構築デスクトップアプリ構築リリース・決済パイプライン販売パートナーお問い合わせ
サービスリモートデスクトップインストールパッケージのカスタマイズ
TASTEDEV
製品

デスクトップツール

TASTECAD複雑な図面を、軽やかにひらくTASTEFILES必要なファイルだけを、最速でTASTEFTPファイルが行き交う、いちばん端正な道TASTESSHサーバーとあなたを、より確かにつなぐTASTEZIPより速くまとめ、より安全にひらくTASTEWINM2つの画面で整理する、キーボード中心のファイルマネージャー

開発ツール

TASTECODERust アプリの編集・ビルド・デバッグ・テストまでひとつのウィンドウでTASTESTUDIO編集からリモートテスト、承認した AI 修正までTASTEETL値を見ながら、データを運ぶ

サーバー

TASTEREMOTE自社サーバーで運用するリモートデスクトップTASTEGEOGeoServer 互換の空間情報サーバーを Rust で作り直しますTASTEMAILメールの主導権を、もう一度自分の手に

投資

TASTESTOCKいつ・何を。自分で決めたルールどおりに動く株式自動売買

はじめる

ダウンロード全製品の最新版ドキュメント各製品の使い方購入サブスクリプション・永久ライセンスすべての製品全製品を一覧で
すべての製品
マーケットプレイスニュース
技術

ニュース

今日の開発ニュース言語・フレームワーク・GitHub・AI の公式発表開発ドキュメント言語・フレームワーク・アルゴリズムのガイド

プログラミング言語

RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDart

フレームワーク

Webフロントエンド フレームワークNext.jsReactVue.js · NuxtSvelteKitAngular
サーバー フレームワークSpring BootDjangoFastAPIASP.NET CoreNestJS · Express
アプリ・デスクトップ フレームワークTauriFlutterReact NativeElectron

アルゴリズム

アルゴリズムの基礎計算量解析 · ソート · 探索 · 基本データ構造 · 木構造とヒープ主要アルゴリズムグラフ探索 · 最短経路と最小全域木 · 動的計画法 · 貪欲法 · 分割統治法 · バックトラッキング発展アルゴリズム文字列アルゴリズム · 数学と整数論 · ビット演算 · セグメント木 · Union-Find実務のアルゴリズムコーディング面接対策 · LRUキャッシュ · レート制限 · コンシステントハッシュ法
開発ドキュメントをすべて見る
コミュニティ
プロジェクトビルダーと作品を探す開発ログ決定と試行錯誤プロンプト再利用できる指示AIツールの問題Claude・Codex・Gemini の問題と回避策ショーケース完成した成果コミュニティガイドオープンにつくる方法
協業
技術移転Rust・egui でネイティブアプリを作るウェブサイト構築10言語、アカウント、ドキュメントとダウンロードデスクトップアプリ構築Rust・egui、WebView なしリリース・決済パイプライン署名・MSI・deb・rpm・AppImage・ハッシュ販売パートナー紹介して決済額の30%お問い合わせ構築・提携・投資のご相談
サービス
リモートデスクトップブラウザから自分のパソコンへインストールパッケージのカスタマイズ必要な機能だけ、設定は事前に
購入
JA
한국어EnglishDeutschEspañolFrançaisItalianoPortuguês (Brasil)日本語简体中文繁體中文
ログイン
ログイン
TASTEDEV

小さなツールから、より良い開発へ。

support@tastedev.netTASTEDEVを支援
製品製品ラインナップ購入ドキュメントダウンロードマーケットプレイスサポート
コミュニティプロジェクト開発ログプロンプトAIツールの問題ショーケース
ポリシー会社概要プライバシーポリシー利用規約サブスクリプション規約返金ポリシーコミュニティガイドライン
アカウントログイン新規登録
訪問者
今日
昨日
累計閲覧
商号
GXSOFT
代表者
CHOI YOUNG GU
事業者登録番号
853-44-00368
所在地
9F V24, 351 Gyeongin-ro, Michuhol-gu, Incheon, Republic of Korea
電話
+82-10-4404-1523
メール
whitecalvin@tastedev.net
個人情報保護責任者
CHOI YOUNG GU

© 2026 TASTEDEV製品情報は検証済みの開発ソースに基づいています。リリース・改善中

ビット演算 ドキュメント
ビット演算
ConceptsHow It WorksImplementationComplexityPractice ProblemsReal-World Use
ディスカッション

ビット演算 ガイド · 4/6

Complexity

この章は現在、英語でのみ提供しています。

A single bitwise operation is one machine instruction, so on fixed-width integers it is O(1). What matters is how many times you repeat it and how many states a mask can describe. This chapter compares popcount strategies, measures subset and submask enumeration, and shows where bitmask DP stops being practical.

Basic operations

On w-bit integers, AND, OR, XOR, NOT, shifts, x & -x and x & (x - 1) all take O(1) time and space. Python integers are unbounded, so strictly an operation costs O(b) for a b-bit value, but anything that fits in 64 bits is effectively constant.

Popcount strategies

MethodTimeNotes
Check every bitO(w)simplest, always w rounds
Kernighan (x &= x - 1)O(k), k = set bitsfast for sparse values, worst case O(w)
8-bit lookup tableO(w / 8)needs a 256-entry table
SWAR parallel sumO(log w)branch-free shifts and adds
Hardware POPCNTO(1)used by int.bit_count, std::popcount, Integer.bitCount
def popcount_swar(x: int) -> int:
    # unsigned 32-bit values only
    x = x - ((x >> 1) & 0x55555555)
    x = (x & 0x33333333) + ((x >> 2) & 0x33333333)
    x = (x + (x >> 4)) & 0x0F0F0F0F
    return ((x * 0x01010101) & 0xFFFFFFFF) >> 24

assert all(popcount_swar(v) == v.bit_count() for v in (0, 1, 255, 2**32 - 1, 0xDEADBEEF))

If you need the popcount of every number from 0 to n, fill a table in O(n) with bits[i] = bits[i >> 1] + (i & 1).

Subset enumeration

With n items there are 2^n masks; testing n bits per mask costs O(2^n * n) time. Collecting every subset in a list also costs O(2^n * n) space, while a generator needs only O(n). Rough limits:

n2^n2^n * nFeel
101,024about 10 thousandinstant
1665,536about 1 millionfast
20
この章について質問がありますか?
前の章Implementation次の章Practice Problems
about 1.05 million
about 21 million
tens of ms in C++, seconds in Python
25about 33.5 millionabout 840 millionimpractical in Python
40about 1.1 trillion-needs meet in the middle

Submasks and 3^n

Looping over every mask and then over its submasks looks like O(4^n), but it is O(3^n): each item is either outside the mask, in the mask only, or in both mask and submask. For n = 15, 3^15 is about 14 million, which is fine.

n = 4
pairs = 0
for mask in range(1 << n):
    sub = mask
    while True:
        pairs += 1
        if sub == 0:
            break
        sub = (sub - 1) & mask
print(pairs, 3 ** n)   # 81 81

Subsets of a fixed size

If you only need subsets of size k, scanning all 2^n masks and filtering by popcount still costs O(2^n). When C(n, k) is much smaller than 2^n, generating combinations directly, as itertools.combinations does, costs O(C(n, k) * k). If you need them as masks, Gosper's hack finds the next mask with the same popcount in O(1).

def next_same_popcount(x: int) -> int:
    low = x & -x
    ripple = x + low
    return (((ripple ^ x) >> 2) // low) | ripple


mask, n = 0b0011, 4
while mask < (1 << n):
    print(format(mask, "04b"), end=" ")   # 0011 0101 0110 1001 1010 1100
    mask = next_same_popcount(mask)

Bitmask DP

With 2^n states and n transitions each, a bitmask DP runs in O(2^n * n). When the state is (set, last vertex), as in the traveling salesman problem (Held-Karp), it is O(2^n * n^2) time and O(2^n * n) space. TSP with n = 20 is about 400 million steps: fine in C++, hard in Python. Compared with trying all n! orders, n = 15 means 1.3 trillion versus about 7.4 million.

import math
for n in (10, 15, 20):
    print(n, math.factorial(n), (1 << n) * n * n)

Comparing set representations

RepresentationMembershipUnionMemoryBest for
Integer bitmaskO(1)O(1)w bitsup to 64 items
Bitset (std::bitset, BitSet)O(1)O(n / w)n bitsthousands to millions of items
Hash set (set)O(1) averageO(n)tens of bytes per itemwide, sparse domains
Sorted listO(log n)O(n)one slot per itemwhen order matters

Summary

Bitwise operations are O(1); the cost comes from repetitions and state counts. Subset enumeration is O(2^n * n), all mask/submask pairs are O(3^n), and bitmask DP is typically O(2^n * n) or O(2^n * n^2). These techniques shine when n is around 20 or less; beyond that, look for a different approach.

コメント 0件

人気のコメント順新しい順

ログイン · ログインするとコメントできます。