時間/数値
ビット演算
指定したビット幅(8/16/32/64ビット)の整数に対するAND/OR/XOR/NAND/NOR/XNOR/NOTおよびシフト演算、任意の進数で表示。
(シフトするビット数、10進数)
Bitwise operations work directly on a number's individual bits rather than its value as a whole — the basis for permission flags, network protocols, and low-level optimization.
How to use it
- Enter one or two numbers in any base (binary, hex, decimal) and pick an operation (AND, OR, XOR, NAND, NOR, XNOR, NOT, or a shift).
- Pick a bit width (8/16/32/64 bits) — it affects how NOT and shifts behave at the number's boundaries.
- The result is shown instantly in several number bases for easy comparison.
Common uses
- Working with permission or setting flags packed into a single number (file system access rights, for example).
- Debugging a network protocol or binary format where fields are packed at the individual-bit level.
- A teaching example for understanding binary number representation and operations on it.
Things to keep in mind
The result of NOT and shifts depends on the chosen bit width — the same number gives a different result at 8-bit versus 32-bit width because of the different number of bits involved.
XOR is often used to toggle a flag — applying XOR twice with the same value returns the original.
よくある質問
AND、OR、XOR、NAND、NOR、XNOR、NOTの違いは何ですか?
ANDは両方のビットが1のときのみ1を返し、ORは少なくとも一方が1のとき1を返し、XORはビットが異なるとき1を返します。NAND/NOR/XNORはそれらの否定形で、NOTは単一のオペランドの各ビットを反転させます。
数値のビット幅と符号はどのように扱われますか?
8/16/32/64ビットの幅を選択してください — 演算とシフトはその範囲内で行われます。負の数は2の補数で表現されるため、10進表記に慣れているとNOTやシフトの結果が予想外に見えることがあります。
左シフトと右シフトの違いは何ですか?
左シフト(<<)は右側にゼロを追加し、実質的にシフト量分だけ2のべき乗を掛けます。右シフトは論理的(左側にゼロを追加)または算術的(符号ビットを保持)であり、種類は選択したモードによって異なります。
"if (a & b == c)"のような式が期待通りに動作しないのはなぜですか?
ほとんどの言語では、比較演算子==はビット演算子&よりも優先順位が高いため、この式は書いた人が意図したものではなく"a & (b == c)"として評価されます。この落とし穴を避けるには、ビット演算を常に明示的な括弧で囲んでください。
個別のブール変数の代わりにビットフラグを使う理由は?
複数のフラグを1つの数値にまとめると、保存や送信がよりコンパクトになります(例えばネットワークプロトコルやファイル形式で)。また、AND/OR/XORで特定のフラグをチェックまたは変更するのは1回の高速な演算で済みます。